我有一张这样的表:
ID A_Kode B_Kode C_Kode
--------------------------------
1 10 12 0
2 15 0 0
3 0 16 17
4 0 0 0
需要提供此结果的查询:
ID Kode
------------
1 10
1 12
2 15
3 16
3 17
4 0
答案 0 :(得分:2)
也许union all
会对你有好处吗?
select ID, A_Kode
from tab
union all
select ID, B_Kode
from tab
where B_Kode <> 0
union all
select ID, C_Kode
from tab
where C_Kode <> 0
order by ID
答案 1 :(得分:0)
首先,您需要UNION存在非零值的所有行,然后添加0,其中所有列值都为零。
select id,A_Kode as Kode from t where A_Kode<>0
union all
select id,B_Kode as Kode from t where B_Kode<>0
union all
select id,C_Kode as Kode from t where C_Kode<>0
union all
select id,0 as Kode from t where A_Kode=0 and B_Kode=0 and C_Kode=0