I’m having some trouble figuring out how construct a series of SQL statements to split some data I have into a per-year basis. There are n items which have a recorded quantity for a set of days across so many years. Here’s is a a small sample layout for the main data I have.
|========|==============|===========|
| Name | Date | Quantity |
|========|==============|===========|
1 | AAAAA | 10-DEC-2008 | 5 |
2 | AAAAA | 11-DEC-2008 | 2 |
3 | AAAAA | 12-DEC-2008 | 0 |
4 | AAAAA | 09-DEC-2009 | 3 |
5 | AAAAA | 10-DEC-2009 | 2 |
6 | AAAAA | 11-DEC-2009 | 3 |
7 | BBBBB | 10-DEC-2008 | 5 |
8 | BBBBB | 11-DEC-2008 | 2 |
9 | BBBBB | 12-DEC-2008 | 0 |
10 | BBBBB | 09-DEC-2009 | 3 |
11 | BBBBB | 10-DEC-2009 | 1 |
12 | BBBBB | 11-DEC-2009 | 0 |
|========|==============|===========|
I need to convert this into a table which takes the form.
|========|==============|===============|===============|
| Name | Date | Quantity 2008 | Quantity 2009 |
|========|==============|===============|===============|
1 | AAAAA | 09-DEC | | 3 |
2 | AAAAA | 10-DEC | 5 | 2 |
3 | AAAAA | 11-DEC | 2 | 3 |
4 | AAAAA | 12-DEC | 0 | |
5 | BBBBB | 09-DEC | | 3 |
6 | BBBBB | 10-DEC | 5 | 1 |
7 | BBBBB | 11-DEC | 2 | 0 |
8 | BBBBB | 12-DEC | 0 | |
|========|==============|===============|===============|
It should be assumed that there are thousands of named items in the database and the start days of recording on each year will be different, E.G., 6th Dec 2010 and 5th Dec 2009.
Hope someone can help.
In case you are using Oracle 11g, you can pivot the table as:
that returns the following