473,383 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Query to sum sales by SKU & portion of date

I am trying to write an Access query that will report sales by SKU for a given month. The data table has the following columns:

Expand|Select|Wrap|Line Numbers
  1. OrderNumber    OrderDate        SKU     Title        QuantityShipped
  2. 10             1/23/2016        XXXX    Product X       1
  3. 11             3/12/2016        XXXX    Product X       1
  4. 12             10/2/2017        XXXX    Product X       1
  5. 13             12/22/2017       XXXX    Product X       1
  6. 14             4/25/2018        XXXX    Product X       3
  7. 15             4/27/2018        XXXX    Product X       1
  8. 16             1/15/2016        YYYY    Product Y       2
  9. 17             5/7/2017         YYYY    Product Y       1
  10. 18             5/18/2017        YYYY    Product Y       1
  11. 19             3/29/2018        YYYY    Product Y       3
I need only one line per SKU per month/year combination, i.e. output similar to this:
Expand|Select|Wrap|Line Numbers
  1. SKU      Title          Date     TotalShipped
  2. XXXX     Product X     Jan-16     1
  3. XXXX     Product X     Mar-16     1
  4. XXXX     Product X     Oct-17     1
  5. XXXX     Product X     Dec-17     1
  6. XXXX     Product X     Apr-18     4
  7. YYYY     Product Y     Jan-16     2
  8. YYYY     Product Y     May-17     2
  9. YYYY     Product Y     Mar-18     3
I can't seem to tease out the date format using DATEPART and I can't seem to isolate the SKU's into one line. Any hlep would be appreciated.
Aug 17 '18 #1

✓ answered by zmbd

NeoPa,
Yet using my original SQL, the first([Odate]) only appears in the ORDER BY clause of the SQL - avoiding the potential "expanded" records (where everything shows up) issue. I stumbled on this a few years back - my guess is that because everything is being grouped at the month and year level then the "First" conditional simply looks at the first record in the aggregate and takes that value for the sort.
Either method works, is reproducible across different datasets, and appears to be reliable.


Re-reading this, I don't think I was very clear:
I also tried with leaving the "First" so that the format by YYYYMM didn't appear in the group by
What I intended to say was - leaving the format(yyyymm) in the first() we still get the proper time-based sort:

Expand|Select|Wrap|Line Numbers
  1. SELECT t_byte971242.SKU, t_byte971242.Title
  2. , Format([Odate],"mmm-yyyy") AS OrdrDate
  3. , Sum(t_byte971242.Qty) AS SumOfQty
  4. FROM t_byte971242
  5. GROUP BY 
  6. t_byte971242.SKU
  7. , t_byte971242.Title
  8. , Format([Odate],"mmm-yyyy")
  9. ORDER BY 
  10. First(t_byte971242.Title)
  11. First(Format([Odate],"yyyymm"));
  12. ^^^^^^^ Not in the Group Clause! ^^^^^^^

10 1690
zmbd
5,501 Expert Mod 4TB
toddi76,
Welcome to Bytes.com!

Normally we ask that you show your work; however, with some of the more complex things, or when the obvious isn't the easiest solution (red-herrings abound in Access - in this case, DatePart()!)... and I've done this type of summary dozens of times in the lab:
Expand|Select|Wrap|Line Numbers
  1. SELECT byte971242.SKU, byte971242.Title
  2.    , Format([Odate],"mmm-yy") AS OrdrDate
  3.    , Sum(byte971242.Qty) AS SumOfQty
  4. FROM byte971242
  5. GROUP BY byte971242.SKU
  6.    , byte971242.Title
  7.    , Format([Odate],"mmm-yy")
  8. ORDER BY First(byte971242.Title)
  9.    , First(byte971242.Odate);
I used this thread's id as a generic table name.
Shortened the [Title] entries a tad for the example :)
Instead of the DatePart() used the simple Format()
should give you (I prefer a 4 digit year the Above SQL will return the 2 digit year as shown in your OP):


The sort order is the tricky part :)
Here I used the [Title]; however, you can just as easily use the [SKU] depending on your requirement/preference.
Attached Images
File Type: jpg 971242_SumQ.JPG (28.5 KB, 294 views)
Aug 18 '18 #2
NeoPa
32,556 Expert Mod 16PB
If you use Format([ODate],'yyyymm') for your ORDER BY clause you should find it a little more straightforward.

You must use the same expression in the GROUP BY clause as you do in the SELECT one or it'll complain.
Aug 18 '18 #3
zmbd
5,501 Expert Mod 4TB
NeoPa,
You are absolutely correct in the GROUP BY; however, the sort is happening in the ORDER BY so there is a bit of SQL-slight of hand being pulled here!

When you use the Format([ODate],'yyyymm') in place of the ORDER BY we then get the alphabetical sort by the Month name of [ODate] instead of the numerical Date sort - Tried this very early on for my lab reports - almost enough to drive a Chemist crazy... as if one could tell the difference @;@ ;

thus,

Either
Expand|Select|Wrap|Line Numbers
  1. SELECT t_byte971242.SKU, t_byte971242.Title, Format([Odate],"mmm-yyyy") AS OrdrDate
  2.   , Sum(t_byte971242.Qty) AS SumOfQty
  3. FROM t_byte971242
  4. GROUP BY t_byte971242.SKU
  5.   , t_byte971242.Title
  6.   , Format([Odate],"mmm-yyyy")
  7. ORDER BY First(t_byte971242.Title)
  8. , First(Format([Odate],"mmm-yyyy"));
OR

Expand|Select|Wrap|Line Numbers
  1. SELECT t_byte971242.SKU, t_byte971242.Title
  2.    , Format([Odate],"mmm-yyyy") AS OrdrDate
  3.    , Sum(t_byte971242.Qty) AS SumOfQty
  4. FROM t_byte971242
  5. GROUP BY t_byte971242.SKU, t_byte971242.Title
  6.    , Format([Odate],"mmm-yyyy")
  7.    , t_byte971242.Title, Format([Odate],"mmm-yyyy")
  8. ORDER BY t_byte971242.Title
  9. , Format([Odate],"mmm-yyyy");
Yield the same alphabetical sort by month name instead of by numerical value (well, sort of... with this data a bit random - ):


Even with:
ORDER BY First(t_byte971242.Title), First(Format([Odate],"mmm-yyyy"));
the alphabetical sort on the month name happens.

As far as the Access-SQL parser is concerned the Format() is returning a string value

However, in VBA - the parser can be told to accept "Jan-2018" as a date value and all is well:




Attached the DB that the screen shots were taken from in the previous post (#2) :)
Attached Images
File Type: jpg 971242_OrdByWFrmt.JPG (28.0 KB, 206 views)
File Type: jpg 971242_POC_DateValue.JPG (38.4 KB, 254 views)
Attached Files
File Type: zip Bytes971242.zip (19.3 KB, 39 views)
Aug 18 '18 #4
NeoPa
32,556 Expert Mod 16PB
Hi Z.
I think you may need to check you're doing that right. Sorting by yyyymm sorts numerically by month - not alphabetically.
LMK if you're still getting wrong results after following my earlier instructions carefully.
Aug 20 '18 #5
zmbd
5,501 Expert Mod 4TB
silly ole bear - I didn't catch the "YYYY" first and the change to "mm" - maybe I need more sleep, or honey.
Expand|Select|Wrap|Line Numbers
  1. SELECT t_byte971242.SKU, t_byte971242.Title
  2. Format([Odate],"mmm-yyyy") AS OrdrDate, Sum(t_byte971242.Qty) AS SumOfQty
  3. FROM t_byte971242
  4. GROUP BY t_byte971242.SKU, t_byte971242.Title
  5. , Format([Odate],"mmm-yyyy"),
  6. Format([Odate],"yyyymm")
  7. ORDER BY First(t_byte971242.Title)
  8. , Format([Odate],"yyyymm");
I also tried with leaving the "First" so that the format by YYYYMM didn't appear in the group by - notice it's not in the select clause... curious that.
does indeed return the numeric sort as the previous...
Aug 20 '18 #6
NeoPa
32,556 Expert Mod 16PB
ZMBD:
I also tried with leaving the "First" so that the format by YYYYMM didn't appear in the group by - notice it's not in the select clause... curious that.
Check back to my earlier post (#3) where I explain why certain things need to go where.

PS. I just looked again and, though I didn't really understand what you were saying, I now think you found the yyyymm was also required in the GROUP BY clause. That's a pain. The reason will be to do with the fact that unless it's in there it doesn't know that it's the same data as the mmm-yyyy and thinks it isn't aggregated unless it's in there. As I say - a pain.

So, it turns out that it needs to be in the GROUP BY clause even if it's in the ORDER BY clause and not the SELECT clause.
Aug 20 '18 #7
zmbd
5,501 Expert Mod 4TB
NeoPa,
Yet using my original SQL, the first([Odate]) only appears in the ORDER BY clause of the SQL - avoiding the potential "expanded" records (where everything shows up) issue. I stumbled on this a few years back - my guess is that because everything is being grouped at the month and year level then the "First" conditional simply looks at the first record in the aggregate and takes that value for the sort.
Either method works, is reproducible across different datasets, and appears to be reliable.


Re-reading this, I don't think I was very clear:
I also tried with leaving the "First" so that the format by YYYYMM didn't appear in the group by
What I intended to say was - leaving the format(yyyymm) in the first() we still get the proper time-based sort:

Expand|Select|Wrap|Line Numbers
  1. SELECT t_byte971242.SKU, t_byte971242.Title
  2. , Format([Odate],"mmm-yyyy") AS OrdrDate
  3. , Sum(t_byte971242.Qty) AS SumOfQty
  4. FROM t_byte971242
  5. GROUP BY 
  6. t_byte971242.SKU
  7. , t_byte971242.Title
  8. , Format([Odate],"mmm-yyyy")
  9. ORDER BY 
  10. First(t_byte971242.Title)
  11. First(Format([Odate],"yyyymm"));
  12. ^^^^^^^ Not in the Group Clause! ^^^^^^^
Aug 21 '18 #8
NeoPa
32,556 Expert Mod 16PB
Yes Zmbd. That's a good solution. We know they're the same - and SQL frankly doesn't need to. Like it :-)
Aug 21 '18 #9
This worked perfectly for me.

Thank you zmbd and NeoPa for your assistance!
Sep 26 '18 #10
NeoPa
32,556 Expert Mod 16PB
Always a pleasure. I've just set Zmbd's last post as Best Answer as it has the actual solution posted in it.
Sep 26 '18 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: S.Patten | last post by:
Hi, I have a problem with updating a datetime column, When I try to change the Column from VB I get "Incorrect syntax near '942'" returned from '942' is the unique key column value ...
1
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to use a make table query to pull the last transactionID, so I can use an append query to reset the transactionID to the next...
7
by: Nicolae Fieraru | last post by:
Hi All, I have a table tblProducts where I have four fields:\ Index, ProductName, EnterDate (as Date/Time - Medium Date), PurchaseDate (Date/Time - Medium Date) The EnterDate is automatically...
2
by: Norbert Lieckfeldt | last post by:
I am setting up a database for a friend who's an Optician, using MS Access 2002. All seems to be working well, but I have hit a snag. There's a calculated field both in a form and a query which...
3
by: olav78 | last post by:
Hi! I have a table named sales with the columns date (timestamp) and price (bigdecimal or something). I want to get the sale for each day (the sum of ‘price’ for all the rows with the same...
16
by: Mik | last post by:
I apologise if this post seems a little basic, but I am a newbie and have NO access knowledge. I have downloaded the Accounts Ledger from the Microsoft Website. It allows the user to review a...
2
seshu
by: seshu | last post by:
Hi Everybody this is seshu here i have a doubt How to write a query to execute on date change (do you call that a batch code or batch id i dont know what is the technical name of such a code )...
1
by: thepresidentis | last post by:
here is my problem, i have a website that compiles concert listings for users to search for shows in their local area, i ran into a problem with returning search results due to an improper format...
5
FOE2272
by: FOE2272 | last post by:
I am working on a report that will Sum the Sales (Bid Price Field) that are still Active (Bid Status Field) for the past 1 Week, 2 Weeks, 3 Weeks, 4 Weeks, 30 Days, 60 Days, 90 Days, 120...
2
by: wevans | last post by:
I have 3 tables, Customer, Offsite Service and service. I need to create a sales report/query based on the results of the user input, which is a date range. I've got it working great with two of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.