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:
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
SELECT byte971242.SKU, byte971242.Title
, Format([Odate],"mmm-yy") AS OrdrDate
, Sum(byte971242.Qty) AS SumOfQty
FROM byte971242
GROUP BY byte971242.SKU
, byte971242.Title
, Format([Odate],"mmm-yy")
ORDER BY First(byte971242.Title)
, 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.
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
SELECT t_byte971242.SKU, t_byte971242.Title, Format([Odate],"mmm-yyyy") AS OrdrDate
, Sum(t_byte971242.Qty) AS SumOfQty
FROM t_byte971242
GROUP BY t_byte971242.SKU
, t_byte971242.Title
, Format([Odate],"mmm-yyyy")
ORDER BY First(t_byte971242.Title)
, First(Format([Odate],"mmm-yyyy"));
OR
Expand|Select|Wrap|Line Numbers
SELECT t_byte971242.SKU, t_byte971242.Title
, Format([Odate],"mmm-yyyy") AS OrdrDate
, Sum(t_byte971242.Qty) AS SumOfQty
FROM t_byte971242
GROUP BY t_byte971242.SKU, t_byte971242.Title
, Format([Odate],"mmm-yyyy")
, t_byte971242.Title, Format([Odate],"mmm-yyyy")
ORDER BY t_byte971242.Title
, 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) :)
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.
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
SELECT t_byte971242.SKU, t_byte971242.Title
, Format([Odate],"mmm-yyyy") AS OrdrDate, Sum(t_byte971242.Qty) AS SumOfQty
FROM t_byte971242
GROUP BY t_byte971242.SKU, t_byte971242.Title
, Format([Odate],"mmm-yyyy"),
Format([Odate],"yyyymm")
ORDER BY First(t_byte971242.Title)
, 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...
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.
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:
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
...
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...
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...
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...
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...
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...
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 )...
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...
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...
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...
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...
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...
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,...
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$) {
}
...
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...
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...
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...