Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 278551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:15:27+00:00 2026-05-12T01:15:27+00:00

A friend asked me for help on building a query that would show how

  • 0

A friend asked me for help on building a query that would show how many pieces of each model were sold on each day of the month, showing zeros when no pieces were sold for a particular model on a particular day, even if no items of any model are sold on that day. I came up with the query below, but it isn’t working as expected. I’m only getting records for the models that have been sold, and I don’t know why.

select days_of_months.`Date`,
       m.NAME as "Model",
       count(t.ID) as "Count"
  from MODEL m
  left join APPLIANCE_UNIT a on (m.ID = a.MODEL_FK and a.NUMBER_OF_UNITS > 0)
  left join NEW_TICKET t on (a.NEW_TICKET_FK = t.ID and t.TYPE = 'SALES'
and t.SALES_ORDER_FK is not null)
 right join (select date(concat(2009,'-',temp_months.id,'-',temp_days.id)) as "Date"
               from temp_months
              inner join temp_days on temp_days.id <= temp_months.last_day
              where temp_months.id = 3 -- March
             ) days_of_months on date(t.CREATION_DATE_TIME) =
date(days_of_months.`Date`)
 group by days_of_months.`Date`,
       m.ID, m.NAME

I had created the temporary tables temp_months and temp_days in order to get all the days for any month. I am using MySQL 5.1, but I am trying to make the query ANSI-compliant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T01:15:27+00:00Added an answer on May 12, 2026 at 1:15 am

    You should CROSS JOIN your dates and models so that you have exactly one record for each day-model pair no matter what, and then LEFT JOIN other tables:

    SELECT  date, name, COUNT(t.id)
    FROM    (
            SELECT ...
            ) AS days_of_months
    CROSS JOIN
            model m
    LEFT JOIN
            APPLIANCE_UNIT a
    ON      a.MODEL_FK = m.id
            AND a.NUMBER_OF_UNITS > 0
    LEFT JOIN
            NEW_TICKET t
    ON      t.id = a.NEW_TICKET_FK
            AND t.TYPE = 'SALES'
            AND t.SALES_ORDER_FK IS NOT NULL
            AND t.CREATION_DATE_TIME >= days_of_months.`Date`
            AND t.CREATION_DATE_TIME < days_of_months.`Date` + INTERVAL 1 DAY
    GROUP BY
            date, name
    

    The way you do it now you get NULL‘s in model_id for the days you have no sales, and they are grouped together.

    Note the JOIN condition:

    AND t.CREATION_DATE_TIME >= days_of_months.`Date`
    AND t.CREATION_DATE_TIME < days_of_months.`Date` + INTERVAL 1 DAY
    

    instead of

    DATE(t.CREATION_DATE_TIME) = DATE(days_of_months.`Date`)
    

    This will help make your query sargable (optimized by indexes)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a friend to help me with web site and I have to
A friend of mine asked to create a static website and I found that
I've been asked to help a friend's company to bring up a web application.
A friend of mine asked me to help him with Swing, and I reached
My friend is making a minesweeper clone and he asked me to help with
A friend of mine asked me the other day if I'm just looking at
A friend asked me how to improve some code with LINQ. How would you
My friend asked me to update a PHP application that his company uses. I
A friend asked me to help him with a simple php script I placed
I can enumerate many features of functional programming, but when my friend asked me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.