Item table:
| Item | Qnty | ProdSched |
| a | 1 | 1 |
| b | 2 | 1 |
| c | 3 | 1 |
| a | 4 | 2 |
| b | 5 | 2 |
| c | 6 | 2 |
Is there a way I can output it like this using SQL SELECT
?
| Item | ProdSched(1)(Qnty) | ProdSched(2)(Qnty) |
| a | 1 | 4 |
| b | 2 | 5 |
| c | 3 | 6 |
Let's hit this in two phases. First, although this is not the exact format you wanted, you can get the data you asked for as follows:
Now, to get the data in the format you desired, one way of accomplishing it is a PIVOT table. You can cook up a pivot table in SQL Server as follows:
You can use
PIVOT
for this. If you have a known number of values to transform, then you can hard-code the values via a static pivot:see SQL Fiddle with Demo
If the number of columns is unknown, then you can use a dynamic pivot:
see SQL Fiddle with Demo