My requirement is to create a view by completely "rotating a table". I have tried many methods including 'Pivot' functions as well. Well, here's my problem in detail
I have a table as follows
- Code: Select all
Product | Ingredients | Perecentage|Min|Max|
-------------------------------------------------------
P100 | A |20 |4 |25 |
P100 | B | 30 |8 |44 |
P100 |C | 33 |7 |50 |
The requirment is to have a view like this, when the 'Product' is specified, the View should look like this
- Code: Select all
Ingredient | A |B | C|
-----------------------------
Percentage|20|30|33 |
Min | 4| 8| 7|
Max |25 |44|50|
I managed to get it for Percentage, though I am yet to figure out how to ge the 'Percentage' in a column.... this is my query
- Code: Select all
SELECT *
FROM (SELECT Ingredient , Percentage
FROM product_table WHERE Product =P164)
PIVOT (SUM(Percentage) AS sum_percent FOR (Ingredient ) IN ('A' AS A, 'B' AS b, 'C' AS C))
this gives the desired result for Percentage - but i am clueless how to do this for all 3 fields : Percentage, Min and Max...
Any Ideas are more than welcome!!!