考虑一下。我的表看起来像这样
ID1 | ID2 | Value |
1 1 Three
1 2 Random
1 3 Words
2 1 Not
2 2 So
2 3 Random
3 1 Why
3 2 No
3 3 Yes
我的输出应该是
ID | Value1 | Value2 | Value3
1 | Three | Random | Words
2 | Not | So | Random
3 | Why | No | Yes
我在找什么?我不需要解决方案,只需要寻找的概念,因为我迷失了。谢谢。
答案 0 :(得分:3)
您要查找的概念是PIVOT
或TRANSPOSE
。尝试在Google中搜索“将行转换为列”。
在oracle中,您的PIVOT查询如下:
SELECT *
FROM (SELECT ID1 ID, VALUE
FROM MY_TABLE)
PIVOT (MIN(VALUE) AS VALUE FOR (ID2) IN ('1' ,'2','3'));
答案 1 :(得分:0)
试试这个。
SQL QUERY
SELECT ID1 As 'ID',
MAX(IF(ID2=1 ,Value,null)) AS 'Value1',
MAX(IF(ID2=2,Value,null)) AS 'Value2',
MAX(IF(ID2=3,Value,null)) AS 'Value3'
FROM MyTable GROUP BY ID1;
或强>
SELECT ID1 AS 'ID',
[1] AS 'Value1',
[2] AS 'Value2',
[3] AS 'Value3'
FROM (
SELECT ID1,ID2,Value
FROM MyTable) x
PIVOT(
MAX(Value)
FOR ID2 IN ([1],[2],[3])
) p
<强> FIND SQL FIDDLE HERE 强>