1

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!

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
Sao Tith
  • 81
  • 7
  • You need to transform data (pivot)! There's no need to export these data into another table. – Maciej Los Jan 14 '15 at 20:34
  • Can you pivot a table in access?? – Sao Tith Jan 14 '15 at 20:35
  • Yes, you can. Please, see my answer. – Maciej Los Jan 14 '15 at 20:37
  • Have a look at PIVOT. When you do your PIVOT you will need to manipulate the date so that it only looks at MM/YYYY otherwise you will end up with a column for every date in your data. Have a go, if you get stuck post your cose and we will have a look. – G B Jan 14 '15 at 20:37

1 Answers1

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

Maciej Los
  • 8,468
  • 1
  • 20
  • 35