只选择具有几个相同字段值的4条记录中的一条(selecting only one of 4 records that have several identical field values)

编程入门 行业动态 更新时间:2024-10-26 14:35:08
只选择具有几个相同字段值的4条记录中的一条(selecting only one of 4 records that have several identical field values)

我正在运行一个带有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;

I'm running a script with a MySQL query (seen below) That works exactly as expected but I need to make one small change due to an issue with our data.

The phone system that this is querying sometimes inserts 3 or 4 records for the same phone call, and we don't know exactly why, but when this runs we only want one record for each phone call. I've decided that this needs to be done by looking at a distinct Calling_number, Start_Time, End_time, and Talk_Time_Seconds. The duplicated/identical rows have the same values for those three fields.

The query itself is exactly what we want so I don't want to have to restructure it much if possible, but I need some help with this.

How can I make this select a distinct record if calling number, start, end and talk time are exactly the same value as stated above?

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;

最满意答案

尝试在四列上使用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;

Try using row_number partition by on the four columns :

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;

更多推荐

本文发布于:2023-07-24 01:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   字段   selecting   records   field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!