分类 单位编码 数量
cxfl dwbm counter
A 1001 10
B 1001 5
B 1002 3
C 1003 2
--------
想通过SQL语句实现如下效果:
假设查询分类是A,B,C(这个不固定),我想让所有的单位编码都有查询的数量,即便分类是0也要显示出如下效果
A 1001 10
B 1001 5
C 1001 0 ----要显示出来
A 1002 0 ----
B 1002 3
C 1002 0 ----
A 1003 0 ----
B 1003 0 ----
C 1003 2
------解决方案--------------------
with t as
(select 'A' cxfl, 1001 dwbm, 10 counter
from dual
union all
select 'B' cxfl, 1001 dwbm, 5 counter
from dual
union all
select 'B' cxfl, 1002 dwbm, 3 counter
from dual
union all
select 'C' cxfl, 1003 dwbm, 2 counter
from dual)
SELECT DISTINCT t1.CXFL,
t2.dwbm,
nvl((select counter
from t t3
where t3.cxfl = t1.cxfl
and t2.dwbm = t3.dwbm),
0)
FROM T t1, t t2
order by dwbm,cxfl;