weixin_39703096 2017-11-09 07:17 采纳率: 0%
浏览 2001

sql语句问题,如何在多表关联下,在重复数据里面,根据某一个唯一的值,sum另一个值的和。

 select distinct cl.description 数据批次,
 s.rolegroupdescription 拨打小组,
 count(distinct oj.customer_guid)分配数量,
sum(case when t1.calltype=2 and t1.stringfield1='CTI' then 1 else 0 end) 拨打次数,
count(distinct case when t4.calltype=2 then t1.customer_guid end) 接通客户数,
sum(case when t4.calltype=2then 1 else 0 end) 接通个数,
sum(case when t1.calltype=2 and t1.stringfield1='CTI'  and floor((t4.endtime- t4.starttime)*24*60)>1 then 1 else 0 end) 一分钟以上个数,
count(distinct co.appl_no) 成交件数,
sum(distinct year_premium) 成交业绩
from customer c
left join (select * from gd_ods_core where cntr_stat in ('K','L','M')) co on co.mobile=c.mobile
inner join objective oj on oj.customer_guid=c.customer_guid
left join event t1 on c.customer_guid=t1.customer_guid
inner join custlists cl on c.stringfield1= cl.custlist_xxx
inner join gd_temp_staff s on oj.tlallot =s.staff_id
inner join (Select customer_guid,max(createddate) as maxcreateddate from objective where tlallot is not null group by customer_guid)L
on oj.customer_guid=L.customer_guid and oj.createddate=L.maxcreateddate
left join record t4 on t4.customer_guid = t1.customer_guid and t4.event_guid = t1.event_guid and t4.staff_id = t1.handleby_id
where
cl.CustList_xxx='CustList_jtkh20170623hh32'
group by cl.description,s.rolegroupdescription
order by cl.description

现在问题是最后取成交金额sum(year_premium) 这里,因为数据重复,所以直接sum会大很多,但如果直接sum(distinct year_premium),会导致不同成交件,但相同交易金额的数給剔除,那就造成金额少了。
gd_ods_core成单表本身是唯一,但由于统计数据的要求,关联了电话拨打表“event”数据分配表“objective”和电话录音表“record”,客户电话拨打得越多,重复数量就越多,如果直接select 成单表gd_ods_core的话,要统计分配数量、拨打次数、接通次数就不准。
请帮忙提供如何根据appl_no(唯一单号)来sum year_premium(业绩金额)
或提供完全不同逻辑
sum(distinct year_premium的情况)
sum(year_premium的情况
两个图都没办法准确统计业绩金额。

  • 写回答

3条回答

  • qq_35631287 2017-11-09 07:33
    关注

    sum另一个值是指的什么

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误