繁体   English   中英

如果列的值大于0,则拆分Sql行

[英]Split Sql Row if Value of a Column is greater than 0

所以目前我有一个表有一堆字段和3个成本字段(人工,零件,杂项)。 我想要做的是将表格分开,以便只有一个成本字段实际上每行有数据。 然后将其他成本字段留空。 例。

Name | Labor |Parts | Misc
test1  800    500     0
test2  0      500     0
test3  700    200     120

to 

Name | Labor |Parts | Misc

test1  800    0       0
test1  0      500     0
test2  0      500     0
test3  700    0       0
test3  0      200     0
test3  0      0     120

我是SQL工作的新手,我找不到一个好的解决方案。 任何想法都会很棒。

谢谢

试试这种方式:

select Name,  Labor, 0 as Parts,0 as Misc
from Table 
where Labor > 0
union all
select Name,   0 as Labor, Parts,0 as Misc
from Table 
where Parts > 0
union all
select Name,  0 as Labor, 0 as Parts,Misc
from Table 
where Misc > 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM