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另一个值是指的什么

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站