sql求和查询语句求助?

来源:百度知道 编辑:UC知道 时间:2024/06/28 11:46:19
nsrsbh nsrmc xssr se sksx sz
420325182462336 供电公司 596.39 346.44 11 1
420325182462336 供电公司 70.20 0.00 21 4
420325182462336 供电公司 56.19 50.54 21 1
420325182462336 供电公司 6.41 74.16 11 4
如何把表中xssr、se按nsrsbh合计,并且xssr合计排除sksx为'21'但se合计不排除sksx为'21',请老师专家帮助?谢谢

哈哈,遇到同行了,哪个局?就给个 Oracle 的吧!
select nsrsbh,nsrmc,sum(decode(sksx,'21',0,xssr)),sum(se)
form [tablename]
group by nsrsbh,nsrmc

识别号不用也没关系!
二楼的也行,没实质差别。

select sum(xssr) * from table where sksx <> '21'
select sum(se) * from table --where 1=1

select nsrsbh,sum(case when sksx='21' then 0 else xssr end) as xssr,sum(se) as se from 表 group by nsrsbh

只能用两次查询了,我的能力范围内

select nsrsbh,sum(xssr) as xssr from AAA where sksx<>21 group by nsrsbh

select nsrsbh,sum(se) as se from AAA group by nsrsbh