读书人

将多条记录合并为一条记录,该如何处理

发布时间: 2012-01-21 21:31:43 作者: rapoo

将多条记录合并为一条记录
表的结构如下
id temp a b c d
---------------------------------------------
1 25 123 123 123 123
1 90 321 321 321 321
2 25 111 111 111 111
查询后我想已这样的格式显示:
id a-25 b-25 c-25 d-25 a-90 b-90 c-90 d-90
-----------------------------
1 123 123 123 123 123 321 321 321
2 111 111 111 111 null null null null

这样的SQL如何写?

[解决办法]
Select
id,
Max(Case [temp] When 25 Then a Else Null End) As [a-25],
Max(Case [temp] When 25 Then b Else Null End) As [b-25],
Max(Case [temp] When 25 Then c Else Null End) As [c-25],
Max(Case [temp] When 25 Then d Else Null End) As [d-25],
Max(Case [temp] When 90 Then a Else Null End) As [a-90],
Max(Case [temp] When 90 Then b Else Null End) As [b-90],
Max(Case [temp] When 90 Then c Else Null End) As [c-90],
Max(Case [temp] When 90 Then d Else Null End) As [d-90]
From

Group By
id

读书人网 >SQL Server

热点推荐