SQL Server 2008 R2 将一行结果拆分为两行
SQL Server 2008 R2 split one row of results into two rows
简单来说,table (t1) 看起来像这样:
id hours dollars
-----------------------
abc 4 40
我想从 table 中获得如下所示的结果:
abcHours 4 0
abcDollars 0 40
谢谢
您可以使用 cross apply
取消透视:
select x.*
from mytable t
cross apply (values
(concat(id, 'Hours' ), hours, 0 ),
(concat(id, 'Dollars'), 0, dollars)
) as x(newid, hours, dollars)
您可以尝试使用运算符 Union 进行简单查询:
Select 'abcHours' as abcHour, hours as Hour, 0 as dollar
Union all
Select 'abcDollars', 0, dollars
简单来说,table (t1) 看起来像这样:
id hours dollars
-----------------------
abc 4 40
我想从 table 中获得如下所示的结果:
abcHours 4 0
abcDollars 0 40
谢谢
您可以使用 cross apply
取消透视:
select x.*
from mytable t
cross apply (values
(concat(id, 'Hours' ), hours, 0 ),
(concat(id, 'Dollars'), 0, dollars)
) as x(newid, hours, dollars)
您可以尝试使用运算符 Union 进行简单查询:
Select 'abcHours' as abcHour, hours as Hour, 0 as dollar
Union all
Select 'abcDollars', 0, dollars