我正在运行带有MySQL查询的脚本(如下所示)完全符合预期,但由于数据存在问题,我需要做一个小改动。
这次查询的电话系统有时会为同一个电话拨打3或4条记录,但我们并不确切知道原因,但是一旦运行,我们只需要为每个电话拨打一条记录。我已经决定这需要通过查看不同的Calling_number, Start_Time, End_time, and Talk_Time_Seconds
来完成。重复/相同的行对于这三个字段具有相同的值。
查询本身正是我们想要的,所以我不想在可能的情况下对其进行重组,但我需要一些帮助。
如果呼叫号码,开始,结束和通话时间与上述值完全相同,如何选择不同的记录呢?
SELECT
FirstN
, LastN
, Extension
, Recieved
, Recieved_Known
, Outbound
, Outbound_Known
, Missed_No_VM
, Missed_VM
, Missed_Known
, Calling_Number
, Called_Number
, Start_Time
, End_Time
, Talk_Time_Seconds
, Hold_Time_Seconds
FROM (
SELECT
firstn
, lastn
, c.extension
, CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
, case when LEGTYPE1 = 2 and answered = 1 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as Recieved_Known
, CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 then 'x' ELSE '' end AS Outbound
, case when LEGTYPE1 = 1 and FINALLYCALLEDPARTYNO = k.phone_number then 'x' ELSE '' end as Outbound_Known
, case when legtype1 = 2 and answered = 0 and finallycalledpartyno not like '%oice%' then 'x' ELSE '' end as Missed_No_VM
, case when finallycalledpartyno like '%oice%' then 'x' ELSE '' end as Missed_VM
, case when ANSWERED = 0 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as Missed_Known
, a.CALLINGPARTYNO AS Calling_Number
, a.FINALLYCALLEDPARTYNO AS Called_Number
, b.starttime as Start_Time
, b.endtime as End_Time
, b.duration as Talk_Time_Seconds
, a.holdtimesecs as Hold_Time_Seconds
FROM ambition.session a
INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
right join jackson_id.users c on a.callingpartyno = c.extension or a.finallycalledpartyno = c.extension
LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
WHERE a.ts >= curdate()
and(a.CALLINGPARTYNO in (select extension from ambition.ambition_users) OR a.finallycalledpartyno IN (select extension from ambition.ambition_users))
) x
order by extension;
答案 0 :(得分:1)
尝试在四列上使用row_number分区:
SELECT
FirstN
, LastN
, Extension
, Recieved
, Recieved_Known
, Outbound
, Outbound_Known
, Missed_No_VM
, Missed_VM
, Missed_Known
, Calling_Number
, Called_Number
, Start_Time
, End_Time
, Talk_Time_Seconds
, Hold_Time_Seconds
FROM (
SELECT
firstn
, lastn
, c.extension
, CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
, case when LEGTYPE1 = 2 and answered = 1 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as Recieved_Known
, CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 then 'x' ELSE '' end AS Outbound
, case when LEGTYPE1 = 1 and FINALLYCALLEDPARTYNO = k.phone_number then 'x' ELSE '' end as Outbound_Known
, case when legtype1 = 2 and answered = 0 and finallycalledpartyno not like '%oice%' then 'x' ELSE '' end as Missed_No_VM
, case when finallycalledpartyno like '%oice%' then 'x' ELSE '' end as Missed_VM
, case when ANSWERED = 0 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as Missed_Known
, a.CALLINGPARTYNO AS Calling_Number
, a.FINALLYCALLEDPARTYNO AS Called_Number
, b.starttime as Start_Time
, b.endtime as End_Time
, b.duration as Talk_Time_Seconds
, a.holdtimesecs as Hold_Time_Seconds
row_number() over (partition by a.CALLINGPARTYNO,b.starttime,b.endtime,b.duration order by 1) rn
FROM ambition.session a
INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
right join jackson_id.users c on a.callingpartyno = c.extension or a.finallycalledpartyno = c.extension
LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
WHERE a.ts >= curdate()
and(a.CALLINGPARTYNO in (select extension from ambition.ambition_users) OR a.finallycalledpartyno IN (select extension from ambition.ambition_users))
) x
where rn = 1
order by extension;