I am currently working on a system that has 2 tables set up like so:
Table_1
-------
ID
Table2ID
Value
Table_2
--------
ID
ColumnName
Some mock results from each table:
Table_1
Table2ID | Value
---------------
1 | ABCD
1 | EFGH
1 | IJKL
2 | MNOP
2 | QRST
2 | UVWX
Table_2
ID | ColumnName
--------------------
1 | First_Set
2 | Second_Set
So, I have the following query, attempting to turn Table_2's row results into columns
SELECT *
FROM(
SELECT B.ColumnName, A.Value
FROM Table_1 AS A
INNER JOIN Table_2 AS B ON A.Table2ID = B.ID
) AS P
PIVOT
(
min(P.Value)
for P.ColumnName in ([First_Set], [Second_Set])
) AS PIV
The problem is that, as it's written, I get back a single result. My returned value would be something like this:
First_Set | Second_Set
-------------------------
ABCD | MNOP
What I want is ALL of the result for each column, but I haven't been able to find a method of using PIVOT that lets me do that.
Does anyone have a suggestion on the best way to turn rows into columns and then receive multiple results for each column?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…