簡體   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