I am trying to pivot a table into a view which would be more easily manipulable with Excel. I am creating views in Sql Server that can be read by the accounting department in order to create stats and other cross-table pivot in Excel.
The table in its current format
OccupationId | ProductId | IsGroup | IsAdditionalNight | Price -------------+-----------+---------+-------------------+------ 1 | 47 | 0 | 0 | 50 2 | 47 | 0 | 1 | 45 3 | 47 | 1 | 0 | 30 4 | 47 | 1 | 1 | 28
The View format that I want to expose
ProductId | Price | AdditionalNight | GroupPrice | GroupAdditionalNight ----------+-------+-----------------+------------+--------------------- 47 | 50 | 45 | 30 | 28
How can I achieve that in Sql Server 2008 R2 ? I need the query to be fast since it will be part of a bigger Query (using CTE).
Currently the only way that I am thinking is to include 4 sub queries to get all 4 prices individually, but I don't like the approach and it scan the table 4 times.