我有一个 SQL Server 2008 表如下...
ID Others1Desc Others1Value Others2Desc Others2value
-- ----------- ------------ ----------- ------------
id_x xyz 12.50 pqr 10.00
id_y abc NULL mno 1.05
现在,我想要如下所示的选择结果...
ID ItemDesc ItemValue
-- ----------- ------------
id_x xyz 12.50
id_x pqr 10.00
id_y abc NULL
id_y mno 1.05
非常感谢任何指导。谢谢
最佳答案
另一种方法是像这样使用cross apply
和values clause
DECLARE @temp table (ID [varchar](100), Others1Desc [varchar](100), Others1Value decimal(11, 2), Others2Desc [varchar](100), Others2Value decimal(11, 2));
INSERT @temp (ID, Others1Desc, Others1Value, Others2Desc, Others2Value) VALUES ('id_x', 'xyz', 12.50, 'pqr', 10.00);
INSERT @temp (ID, Others1Desc, Others1Value, Others2Desc, Others2Value) VALUES ('id_y', 'abc', NULL, 'mno', 1.05);
select ID, t.* from @temp
cross apply
(
values (Others1Desc, Others1Value),
(Others2Desc, Others2Value)
) t(ItemDesc, ItemValue)
结果
ID ItemDesc ItemValue
--------------------------
id_x xyz 12.50
id_x pqr 10.00
id_y abc NULL
id_y mno 1.05
关于sql - 选择 SQL 列作为添加的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842355/