一个sql语句 怎么写比较好

原因:如果这么写,CAPACITY 就计算错了.因为对应一个user_no来说,可能在Df_Money_Files_h存在2条记录,那么在TRANS_RUN 表中能取到2个值,那样不是我期待的结果,我系统TRANS_RUN有几个user_no 统计几个capacity. select count( distinct a.user_no), sum(nvl((SELECT T.CAPACITY FROM TRANS_RUN T WHERE t.trans_state <> 'S' and T.USER_NO =a.user_no),0)), sum(nvl((SELECT sum(nvl(T.CAPACITY,0)) FROM TRANS_RUN T WHERE t.trans_state <> 'S' and T.USER_NO = a.user_no),0)) as CAPACITY , round(sum(nvl(a.total_power,0))/10000,0)--根据需求 不带小数点 from Df_Money_Files_h a,df_write_sect b,busi_code_refer c where a.write_sect_no = b.write_sect_no and b.business_place_code = c.business_place_code and c.arg_busi_place_code = 'KFDLJN01' and a.mon = b.mon and b.mon =TO_DATE('2005-08-01','YYYY-MM-DD') and a.status in ('C','A','P') and a.price_code not in ('000') -- 排除不计费 and a.user_no not in (select user_no from ds_ms_point) --不含趸售 group by nvl(a.trade_type_code,'9910'); 没有办法,最好只能暂时这样解决了;用下面的sql,虽然效率底下: select max(w.row_sn),max(w.trade_type_code),max(w.row_title), sum (w.user_no) as user_num ,sum(w.capacity)as capacity,sum(w.total_power) as total_power from ( select max((select e.row_sn from report_row_info e where e.row_index_code = nvl(a.trade_type_code,'9910') and e.report_code = 'DFREPORT_HYYDFLB')) as row_sn, max(nvl(a.trade_type_code,'9910')) as trade_type_code, max((select e.row_title from report_row_info e where e.row_index_code = nvl(a.trade_type_code,'9910') and e.report_code = 'DFREPORT_HYYDFLB')) as row_title , count( distinct a.user_no) as user_no, ( nvl((SELECT sum(nvl(T.CAPACITY,0)) FROM TRANS_RUN T WHERE t.trans_state <> 'S' and a.USER_NO = t.user_no ),0) ) as capacity, round(sum(nvl(a.total_power,0))/10000,0) as total_power --根据需求 不带小数点 from df_money_files_h a,df_write_sect b,busi_code_refer c where a.write_sect_no = b.write_sect_no and b.business_place_code = c.business_place_code and c.arg_busi_place_code = 'KFDLJN01' and a.mon = b.mon and b.mon = TO_DATE('2005-08-01','YYYY-MM-DD') and a.status in ('C','A','P') and a.price_code not in ('000') -- 排除不计费 and a.user_no not in (select user_no from ds_ms_point) --不含趸售 group by a.user_no) w group by nvl(w.trade_type_code,'9910');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值