I have a table like this
Id | Year | Category | Type | Item | Price |
---|---|---|---|---|---|
1 | 2010 | Cloth | Small | Red | 25 |
2 | 2010 | Cloth | Large | Blue | 30 |
3 | 2010 | Laptop | Small | Blue | 15 |
4 | 2011 | Cloth | Small | Red | 22 |
5 | 2011 | Cloth | Large | Blue | 28 |
6 | 2011 | Laptop | Large | Red | 33 |
7 | 2012 | Laptop | Small | Blue | 35 |
I want the output in this format. I want to bring year as columns and display the prices for those years.
Important Note: In output, all years have price and other data. In this example, it would be long, so I have kept only few rows. But in my database, basically all years will have data. There will be price for each year for each category, type and Item
Some important things to note:
- This table has over 50,000 records
- There are maximum 2 possible value for Category i.e Cloth and Laptop. Similarly 2 for Type like Small and Large. And for item also 2 i.e Red and Blue. But for Year it can be 2000 to 2021.
How can I select the records and display the result in this way in MS-SQL ?