我使用oracle环境比如我要查询某子公司的收入情况,如果该子公司没收入就返回子公司名称,请问如何实现?如:
table1
gsdm gsmc
1100 汉口分公司
1200 武昌分公司
1300 汉阳分公司 table2
gsdm sr
1100 40000
1200 60000
1300 20000
1300 10000 select a.gsmc,sum(b.sr) as se from table1 a,table2 b where a.gsdm='1300' and se>30000 查询汉阳分公司收入总额是否>30000,需要显示出以下结果:(不要显示其他公司的名称) a.gsmc se
汉阳分公司 0
table1
gsdm gsmc
1100 汉口分公司
1200 武昌分公司
1300 汉阳分公司 table2
gsdm sr
1100 40000
1200 60000
1300 20000
1300 10000 select a.gsmc,sum(b.sr) as se from table1 a,table2 b where a.gsdm='1300' and se>30000 查询汉阳分公司收入总额是否>30000,需要显示出以下结果:(不要显示其他公司的名称) a.gsmc se
汉阳分公司 0
select a.gsmc,case when sum(b.sr)==null then 0 else sum(b.sr) end as se from table1 a,table2 b where a.gsdm=b.gsdm and a.gsdm='1300' and se>30000
这个语句能执行?聚合函数,不能用在WHERE子句里。
你需要判断其总收入,如果大于某个值,那么返回总收入,如果小于,则返回0。
select a.gsmc,case when sum(b.sr)>30000 then sum(b.sr) else 0 end as se from t1 a,t2 b
where a.gsdm=b.gsdm and a.gsdm='1300'
group by a.gsmc;
2 DECODE(SIGN(SUM(SE)-30000),1,SUM(SE),0) "SUM_SE"
3 FROM TABLE1 T1,
4 TABLE2 T2
5 WHERE T1.GSDM = T2.GSDM(+)
6 AND T1.GSDM = '1300'
7 GROUP BY T1.GSDM,T1.GSMC;GSMC SUM_SE
---------- ----------
汉阳分公司 0SQL>
(
select sum(SR) as SUM_SR from TABLE2 a where a.GSDM='1300'
)b, TABLE1 c where c.GSDM='1300'
---执行结果-----
GSMC SR
1 汉阳分公司 0
---------------