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 7679521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:53:49+00:00 2026-05-31T17:53:49+00:00

I’m looking to make some bar graphs to count item sales by day, month,

  • 0

I’m looking to make some bar graphs to count item sales by day, month, and year. The problem that I’m encountering is that my simple MySQL queries only return counts where there are values to count. It doesn’t magically fill in dates where dates don’t exist and item sales=0. This is causing me problems when trying to populate a table, for example, because all weeks in a given year aren’t represented, only the weeks where items were sold are represented.

My tables and fields are as follows:

items table: account_id and item_id 
// table keeping track of owners' items
items_purchased table: purchaser_account_id, item_id, purchase_date
// table keeping track of purchases by other users
calendar table: datefield
//table with all the dates incremented every day for many years

here’s the 1st query I was referring to above:

 SELECT COUNT(*) as item_sales, DATE(purchase_date) as date
 FROM items_purchased join items on items_purchased.item_id=items.item_id 
 where items.account_id=125
 GROUP BY DATE(purchase_date) 

I’ve read that I should join a calendar table with the tables where the counting takes place. I’ve done that but now I can’t get the first query to play nice this 2nd query because the join in the first query eliminates dates from the query result where item sales are 0.

here’s the 2nd query which needs to be merged with the 1st query somehow to produce the results i’m looking for:

SELECT calendar.datefield AS date, IFNULL(SUM(purchaseyesno),0) AS item_sales
FROM items_purchased join items on items_purchased.item_id=items.item_id 
RIGHT JOIN calendar ON (DATE(items_purchased.purchase_date) = calendar.datefield)
WHERE (calendar.datefield BETWEEN (SELECT MIN(DATE(purchase_date)) 
FROM items_purchased) AND (SELECT MAX(DATE(purchase_date)) FROM items_purchased)) 
GROUP BY date
// this lists the sales/day
// to make it per week, change the group by to this: GROUP BY week(date)

The failure of this 2nd query is that it doesn’t count item_sales by account_id (the person trying to sell the item to the purchaser_account_id users). The 1st query does but it doesn’t have all dates where the item sales=0. So yeah, frustrating.

Here’s how I’d like the resulting data to look (NOTE: these are what account_id=125 has sold, other people many have different numbers during this time frame):

2012-01-01     1
2012-01-08     1
2012-01-15     0
2012-01-22     2
2012-01-29     0

Here’s what the 1st query current looks like:

2012-01-01     1
2012-01-08     1
2012-01-22     2

If someone could provide some advice on this I would be hugely grateful.

  • 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-31T17:53:50+00:00Added an answer on May 31, 2026 at 5:53 pm

    I’m not quite sure about the problem you’re getting as I don’t know the actual tables and data they contain that generates those results (that would help a lot!). However, let’s try something. Use this condition:

    where (items.account_id = 125 or items.account_id is null) and (other-conditions)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.