问题
Back again with an issue that has been bugging me for sometime now. Here's my dilemma. I have a table looks like this
| ID | DATE | SALES |
1 2012-01-20 100.45
1 2012-01-22 55.45
1 2012-02-15 66.77
3 2012-01-19 33.33
3 2012-02-19 69.44
4 2012-02-22 77.55
etc
So what i need to do is make a new table with all the ID grouped in one, and then adding their sales in the month range (i.e 2012-01-01
to 2012-01-30
)
The new table would looke like this
ID | 01-2012 | 02-2012 | 03-2012 | etc | etc
1 | 155.90 | 66.77 |
3 | 33.33 | 69.44 |
4 | 000.00 | 77.55 |
All the recoods need to display even if they sum up to none
any input or help is much appreciated! Thank you internet!
回答1:
You can pivot data by using Transform command.
TRANSFORM SUM(SALES)
SELECT ID, FORMAT([DATE], 'yyyy-MM')
FROM YourTableName
WHERE [DATE] BETWEEN #Date1# AND #Date2#
GROUP BY ID, FORMAT([DATE], 'yyyy-MM')
Replace Date1 and Date2 with proper dates (in ISO format: MM/dd/yyyy).
By The Way: never use reserved words as a column name!
See: SQL Reserved Words
来源:https://stackoverflow.com/questions/27951872/ms-access-database-broken-into-month