sql server 2008 r2将一行结果拆分为两行

shstlldc  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(489)

简单来说,表(t1)如下所示:

id     hours    dollars
-----------------------
abc     4         40

我想从如下表格中得到结果:

abcHours      4      0
abcDollars    0      40

谢谢

v64noz0r

v64noz0r1#

您可以尝试使用运算符联合进行简单查询:

Select  'abcHours' as  abcHour, hours as Hour, 0 as dollar
Union all
Select  'abcDollars', 0, dollars
z9zf31ra

z9zf31ra2#

你可以用 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)

相关问题