请参阅下表。
Sr.No Field1 Field2 Field3
----------------------------------
1. ABC abc@abc HTML
2. ABC efg@efg XML
必需的输出:
Sr.No Field1 HTML XML
--------------------------------------
1. ABC abc@abc efg@efg
感谢。
答案 0 :(得分:0)
你可以这样做吗
DECLARE @T TABLE
(
SeqNo INT IDENTITY(1,1),
Col1 NVARCHAR(255),
Col2 NVARCHAR(255),
Col3 NVARCHAR(255)
)
INSERT INTO @T
(
Col1,
Col2,
Col3
)
VALUES('ABC','abc@abc','HTML'),
('ABC','efg@efg','XML')
SELECT
T1.SeqNo,
T1.Col1,
HTML = T1.Col2,
[XML] = T2.Col2
FROM @T T1
LEFT JOIN @T T2
ON T1.Col1 = T2.Col1
AND T2.Col3 = 'XML'
WHERE T1.Col3 = 'HTML'
或使用PIVOT
像这样
SELECT
SeqNo = MIN(SeqNo),
Col1 = Col1,
HTML = MAX(Html),
[xml] = MAX([XML])
FROM @T
PIVOT
(
MAX(Col2)
FOR
COL3
IN
(
[HTML],[XML]
)
)Q
GROUP BY Col1
但我认为第一个解决方案应该有效
输出
答案 1 :(得分:0)
SELECT `T1`.`Sr.No`, `T1`.`Field1`, `T1`.`Field2` AS HTML,
(SELECT `T2`.`Field2` AS XML FROM TABLE_NAME T2
WHERE `T2`.`Field3` = 'XML' AND `T2`.`Field1` = `T1`.`Field1`)
FROM TABLE_NAME T1 WHERE `T1`.`Field3` = 'HTML'