1

我有一张这样的桌子:

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
4

2 回答 2

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
于 2013-07-22T11:18:43.187 回答
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
于 2013-07-22T11:34:04.203 回答