[英]Combine two rows in single column with same Id
This is currently how I return values. 目前,这就是我返回值的方式。 but I want to combine rows that have the same Id's.
但我想合并具有相同ID的行。 I tried using the unpivot but I couldn't get it to work.
我尝试使用unpivot,但无法正常工作。 here's my query:
这是我的查询:
SELECT * FROM
(
SELECT
ID,
Setup,
Message
FROM myViewHere
)
AS tempTable
UNPIVOT
(
[Message]
FOR Test
IN (ID, Setup)
) AS PVT
This is the result of myViewHere 这是myViewHere的结果
|ID | Setup | Message |
|---|--------|----------|
| 1 | Header | myHeader |
|---|--------|----------|
| 1 | Footer | myFooter |
What I want to achieve: 我要实现的目标:
|ID | Header | Footer |
|---|----------|----------|
| 1 | myHeader | myFooter |
|---|----------|----------|
Typical pivot case https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx 典型的枢轴案例https://technet.microsoft.com/zh-cn/library/ms177410(v=sql.105).aspx
Assumed table name is "source" 假设表名称是“源”
SELECT ID, [Header], [Footer]
FROM source
PIVOT
( max(Message) FOR Setup IN ([Header], [Footer])) p;
See test on sqlfeedle http://sqlfiddle.com/#!6/7635f/7 参见sqlfeedle上的测试http://sqlfiddle.com/#!6/7635f/7
you can change your view query to be like below 您可以将视图查询更改为如下所示
SELECT * FROM
(
SELECT
ID,
Setup,
Message
FROM myViewHere
)
AS tempTable
UNPIVOT
(
[Message]
FOR Test
IN (ID, Setup)
) AS UNPVT
PIVOT
(
MAX([Message])
FOR [Setup]
IN ([Header], [Footer])
)PVT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.