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

  • Home
  • SEARCH
  • 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 7684345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:00:13+00:00 2026-05-31T19:00:13+00:00

I have a query that I’d like to change so that it gives me

  • 0

I have a query that I’d like to change so that it gives me the counts for the current 52 weeks. This query makes use of a calendar table I’ve made which contains a list of dates in a fixed range. The query as it stands is selecting max and min dates and not necessarily the last 52 weeks.

I’m wondering how to keep my calendar table current such that I can get the last 52-weeks (i.e, from right now to one year ago). Or is there another way to make the query independent of using a calendar table?

Here’s the query:

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 week(date)

thoughts?

  • 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-31T19:00:14+00:00Added an answer on May 31, 2026 at 7:00 pm

    Some people dislike this approach but I tend to use a dummy table that contains values from 0 – 1000 and then use a derived table to produce the ranges that are needed –

    CREATE TABLE dummy (`num` INT NOT NULL);
    INSERT INTO dummy VALUES (0), (1), (2), (3), (4), (5), .... (999), (1000);
    

    If you have a table with an auto-incrementing id and plenty of rows you could generate it from that –

    CREATE TABLE `dummy`
    SELECT id AS `num` FROM `some_table` WHERE `id` <= 1000;
    

    Just remember to insert the 0 value.

    SELECT CURRENT_DATE - INTERVAL num DAY
    FROM dummy
    WHERE num < 365
    

    So, applying this approach to your query you could do something like this –

    SELECT WEEK(calendar.datefield) AS `week`, IFNULL(SUM(purchaseyesno),0) AS item_sales
    FROM items_purchased join items on items_purchased.item_id=items.item_id 
    RIGHT JOIN (
        SELECT (CURRENT_DATE - INTERVAL num DAY) AS datefield
        FROM dummy
        WHERE num < 365
    ) AS calendar ON (DATE(items_purchased.purchase_date) = calendar.datefield)
    WHERE calendar.datefield >= (CURRENT_DATE - INTERVAL 1 YEAR)
    GROUP BY week(datefield) -- shouldn't this be datefield instead of date?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have query that gives back results like this: Client1 Product1 $500 Client1 Product2
I have query that looks like this declare @tmpTable Table(id int) insert into @tmpTable(id)
I have a query that creates a result set like this: Rank Name 1
I have a query that looks like this SELECT * from myTable WHERE Date
I have this query that returns the correct data, but I would like to
I have a query that looks like this: using (MyDC TheDC = new MyDC())
I have a query that looks like this WITH CTE AS ( SELECT ROW_NUMBER()
I have a query that looks like this: var TheQuery = (from t in
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)

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.