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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:48:08+00:00 2026-05-31T15:48:08+00:00

I have a large number of records with a transaction datetime field going back

  • 0

I have a large number of records with a transaction datetime field going back several years. I would like to do a comparative analysis between the same timespan this year and last. How can I group by week over a 3 month range?

I’m running into problems using the YEARWEEK and WEEK functions because of the day the year 2012 starts of versus the day 2011 starts on.

Given that I have records with datetimes everyday from Jan 1st to the current day, and records with the same datetimes from the prior year, how can I group by week so the output is sums with dates like: 01/01/2011, 01/08/2011, 01/15/2011, etc., and 01/01/2012, 01/08/2012, 01/15/2012, etc.?

My query so far is as follows:

SELECT 
    DATE_FORMAT(A.transaction_date, '%Y-%m-%d') as date,
    ROUND(sum(A.quantity), 3) AS quantity,
    ROUND(sum(A.total_amount), 3) AS amount,
    A.product_code, 
    D.fuel_type_code, 
    D.fuel_type_name, 
    C.customer_code, 
    C.customer_name 
FROM 
    cl_transactions AS A
INNER JOIN 
    card AS B ON A.card_number=B.card_number 
INNER JOIN 
    customer AS C ON B.customer_code=C.customer_code 
INNER JOIN 
    fuel_type AS D ON A.fuel_type=D.fuel_type_code 
WHERE 
    ((A.transaction_date >= DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y-%m-01')) OR (A.transaction_date - INTERVAL 1 YEAR >=  DATE_FORMAT(NOW() - INTERVAL 15 MONTH, '%Y-%m-01') AND A.transaction_date <= NOW() - INTERVAL 1 YEAR))
GROUP BY 
    A.transaction_date, fuel_type_code;

I would essentially like something that achieves the following pseudo-query:

GROUP BY 
    STARTING FROM THE OLDEST DATE (A.transaction_date + INTERVAL 6 DAY)
  • 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-31T15:48:09+00:00Added an answer on May 31, 2026 at 3:48 pm

    I started with an inner query using sqlvariables to build out from/to ranges for this year and last year of each respective start of year/month/day (ex: 2012-01-01 and 2011-01-01 respectively). From that, I’m also pre-formatting the date for final output so you have ONE master date basis for display reflecting that of whatever the “this year” week would be.

    From that, I do a join to the transaction table where the transaction date is BETWEEN the respective start of current week and start of next week. Since date/time stamps include hour minute, 2012-01-01 by itself is implied as 12:00:00am (midnight) of the day. and between will go UP TO 7 days later 12:00:00 am. And that date will become the start date of the following week.

    So, by joining on the date being between EITHER last yr or this yr time period, its the same group qualification. So the field selection does a ROUND( SUM( IF() )) per respective last year or this year. if the incoming transaction date is LESS than the current year’s week start, then it must be a record from prior year, otherwise its for the current year. So, respectively, add the value itself, or zero as it applies.

    So now, you have the group by. The week that it qualified for was already prepared from the inner query via “ThisYearWeekOf” formatted column, regardless of the otherwise computed “YEARWEEK()” or “WEEK()”. The date ranges took care of that qualification for us.

    Finally, I added the fuel-type as a join and included that as the group by. You have to group by all non-aggregate columns for proper SQL, although MySQL lets you get by by just grabbing the first entry for the given group if it is NOT so specified in group by.

    To close, I DID include the information for the customer as you didn’t have it in the group by and did not appear to be applicable… it would just arbitrarily grab one. However, I’ve added it to the group by, so now your records will show at the per customer level, per product and fuel type, how much sales and quantity between this year and last.

    SELECT
          JustWeekRange.ThisYearWeekOf,
          CTrans.product_code,
          FT.fuel_type_code, 
          FT.fuel_type_name, 
          C.customer_code, 
          C.customer_name,
          ROUND( SUM( IF( CTrans.transaction_date < JustWeekRange.ThisYrWeekStart, CTrans.Quantity, 0 )), 3) as LastYrQty,
          ROUND( SUM( IF( CTrans.transaction_date < JustWeekRange.ThisYrWeekStart, CTrans.total_amount, 0 )), 3) as LastYrAmt,
          ROUND( SUM( IF( CTrans.transaction_date < JustWeekRange.ThisYrWeekStart, 0, CTrans.Quantity )), 3) as ThisYrQty,
          ROUND( SUM( IF( CTrans.transaction_date < JustWeekRange.ThisYrWeekStart, 0, CTrans.total_amount )), 3) as ThisYrAmt,
       FROM 
          ( SELECT 
                     DATE_FORMAT(@ThisYearDate, '%Y-%m-%d') as ThisYearWeekOf,
                     @LastYearDate as LastYrWeekStart,
                     @ThisYearDate as ThisYrWeekStart,
                     @LastYearDate := date_add( @LastYearDate, interval 7 day ) LastYrStartOfNextWeek,
                     @ThisYearDate := date_add( @ThisYearDate, interval 7 day ) ThisYrStartOfNextWeek
                FROM 
                     (select @ThisYearDate := '2012-01-01',
                             @LastYearDate := '2011-01-01' ) sqlvars,
                     cl_transactions justForLimit
                HAVING
                   ThisYrWeekStart < '2012-04-01'
                LIMIT 15 ) JustWeekRange
    
          JOIN cl_transactions AS CTrans
             ON    CTrans.transaction_date BETWEEN 
                   JustWeekRange.LastYrWeekStart AND JustWeekRange.LastYrStartOfNextWeek
               OR  CTrans.transaction_date BETWEEN 
                   JustWeekRange.ThisYrWeekStart AND JustWeekRange.ThisYrStartOfNextWeek
    
          JOIN fuel_type FT
             ON CTrans.fuel_type = FT.fuel_type_code
    
          JOIN card 
             ON CTrans.card_number = card.card_number 
             JOIN customer AS C 
                ON card.customer_code = C.customer_code 
    
       GROUP BY
          JustWeekRange.ThisYearWeekOf,
          CTrans.product_code,
          FT.fuel_type_code, 
          FT.fuel_type_name,
          C.customer_code, 
          C.customer_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large number of records (10,000, increasing every day) that essentially is
I have a database with a large number of records that are Date/Time stamped.
I have a jqGrid with which users will select records. A large number of
I have a large number of csv files that look like this below: xxxxxxxx
I have a large number of instances of a C structure like this: struct
I have a large number of records (around 40,000) in a csv file to
I have a large number of records of the following type, which I have
I have logic consisting of selecting a large number of records from one system,
I have a Rails app that processes a large (millions) number of records in
I have a large number of records, say around 4,000,000, that I want to

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.