所以我有一个包含以下列的表:
Type Test Min Max
-----------------------------
1 a 1 2
1 b Null Null
2 a 0 Null
2 b Null 1
试图让所有人都像这样
Type Test1 Test1 min Test1max Test2 Test2min Test2max
------------------------------------------------------------
1 a 1 2 b Null Null
2 a 0 Null b Null 1
在我使用数据透视表之前首先尝试使用unpivot,但它仍然提供重复测试并删除null任何帮助都非常感谢 还需要显示空值
Select type, result
(
From
Select type, min
From table t
)
Unpivot
(result for minmax in (min1)
)
选择 感谢
答案 0 :(得分:0)
使用row_number()
和条件聚合:
select
[Type]
, Test_1 = max(case when rn = 1 then Test end)
, Test_1_Min = max(case when rn = 1 then Min end)
, Test_1_Max = max(case when rn = 1 then Max end)
, Test_2 = max(case when rn = 2 then Test end)
, Test_2_Min = max(case when rn = 2 then Min end)
, Test_2_Max = max(case when rn = 2 then Max end)
from (
select *
, rn = row_number() over (partition by [Type] order by [Test])
from t
) as s
group by s.Type
rextester演示:http://rextester.com/BKL48976
返回:
+------+--------+------------+------------+--------+------------+------------+
| Type | Test_1 | Test_1_Min | Test_1_Max | Test_2 | Test_2_Min | Test_2_Max |
+------+--------+------------+------------+--------+------------+------------+
| 1 | a | 1 | 2 | b | NULL | NULL |
| 2 | a | 0 | NULL | b | NULL | 1 |
+------+--------+------------+------------+--------+------------+------------+
答案 1 :(得分:0)
select *
from table t1
join table t2
on t1.type = t2.type
and t1.test < t2.test