更新和插入,重复键

时间:2017-08-08 12:42:17

标签: mysql sql duplicates

我在过去几天对此进行了一些更改,但我仍然遇到问题而且无法产生正确的结果。

以下查询用于通过电话分机和日期保留CSR代理的日志。

目标:当它在一天中运行时,它应该为当天表中尚未添加的扩展添加任何信息,并更新现有表的指标。如果代理商1-5整天都在电话上,那么他们的指标应该不断更新,如果代理商6在下午3点接通电话,那么他们应该在那时插入并在当天的其余时间更新。但是,当第二天开始时,我想要重新开始所有代理的新记录,所以在8/8/17之后,那天的记录应该保留并且永远不会被覆盖,因此我们可以在此表中保留历史记录。

我想知道我的问题是否与我的重复密钥更新有关。争论,但我对这个陈述是新的,所以我不确定。我的主要关键是'扩展'但是'扩展',' ExtID'和' Record_date'都是非NUll。我还在' Extension'上添加了一个独特的索引。和' Record_date'进行重复密钥更新'认识到这两个领域,我只是不确定它是否有效。

我只想更新当天,并在第二天开始新记录/新插入。即,如果6名代理人每天工作四天,我希望该表在第4天结束时有24条记录,我只想更新每日记录。

感谢您提供任何有用的信息。这是查询:

    Insert into test.ambition_test(Extension, ExtID, Total_Talk_Time_seconds,
Total_Talk_Time_minutes,Total_Outbound, Total_Inbound, 
Missed_Calls, Total_Calls, Date_of_report, Time_of_report )  
SELECT 
 c.extension as Extension
,RESPONSIBLEUSEREXTENSIONID as ExtID
, sum(Duration) as Total_Talk_Time_seconds
, round(sum(Duration) / 60,2) as Total_Talk_Time_minutes
, sum(if(LEGTYPE1 = 1,1,0)) as Total_Outbound   
, sum(if(LEGTYPE1 = 2,1,0)) as Total_Inboundambition_test
, sum(if(Answered = 1,0,1)) as Missed_Calls
, count(DISTINCT b.NOTABLECALLID) as Total_Calls
, date(now())
,NOW()
FROM cdrdb.session a
LEFT JOIN cdrdb.callsummary b
        ON a.NOTABLECALLID = b.NOTABLECALLID
LEFT join cdrdb.mxuser c
      ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
WHERE b.ts >= curdate()
AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
 group by c.extension
ON duplicate key update Total_Talk_Time_seconds =values(Total_Talk_Time_seconds), 
Total_Talk_Time_minutes =values(Total_Talk_Time_minutes), 
Total_Outbound = values(Total_Outbound), Total_Inbound = values(Total_Inbound), 
Missed_calls = values(Missed_Calls), 
Total_Calls = values(Total_Calls), 
Date_of_report = values(Date_of_report),
Time_of_report = values(Time_of_report); 

enter image description here

2 个答案:

答案 0 :(得分:1)

首先,您需要一个包含日期的列,因此您可以在表DATE(没有DATETIME)的test.ambition_test表中记录每一天的记录。我想你已经创建了它,它叫做Record_Date。

然后你需要在列的表test.ambition_test上创建一个唯一的索引(Extension,Record_Date)(如果你把它作为主键就可以。你需要确保你没有另一个唯一的该表上的键。您可以拥有更多索引,但不是唯一的(既不是主键)。

然后你可以使用insert子句。看到我已经通过DATE(NOW())更改了now()指令,所以你只能得到这一天。 在查询中,您使用Time_of_report,我认为它应该是Record_Date(它必须与索引中的相同)

Insert into test.ambition_test(Extension, ExtID, Total_Talk_Time_seconds,
    Total_Talk_Time_minutes,Total_Outbound, Total_Inbound, 
    Missed_Calls, Total_Calls, Time_of_report,Date_of_report  )  
SELECT 
     c.extension as Extension
    ,RESPONSIBLEUSEREXTENSIONID as ExtID
    , sum(Duration) as Total_Talk_Time_seconds
    , round(sum(Duration) / 60,2) as Total_Talk_Time_minutes
    , sum(if(LEGTYPE1 = 1,1,0)) as Total_Outbound   
    , sum(if(LEGTYPE1 = 2,1,0)) as Total_Inboundambition_test
    , sum(if(Answered = 1,0,1)) as Missed_Calls
    , count(DISTINCT b.NOTABLECALLID) as Total_Calls
    , NOW()
    , DATE(NOW()) 
    FROM cdrdb.session a
    LEFT JOIN cdrdb.callsummary b
            ON a.NOTABLECALLID = b.NOTABLECALLID
   LEFT join cdrdb.mxuser c
          ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
   WHERE b.ts >= curdate()
   AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
    group by c.extension
ON duplicate key update Total_Talk_Time_seconds =values(Total_Talk_Time_seconds), 
    Total_Talk_Time_minutes =values(Total_Talk_Time_minutes), 
    Total_Outbound = values(Total_Outbound), Total_Inbound = values(Total_Inbound), 
    Missed_calls = values(Missed_Calls), 
    Total_Calls = values(Total_Calls), 
    Time_of_report = values(Time_of_report); 

答案 1 :(得分:1)

我对查询做了一些更改,我修改了别名,我认为现在应该没问题了。试试吧:

Insert into test.ambition_test(Extension, ExtID, Total_Talk_Time_seconds,
    Total_Talk_Time_minutes,Total_Outbound, Total_Inbound, 
    Missed_Calls, Total_Calls, Time_of_report,Date_of_report  )  
SELECT 
     c.extension 
    ,RESPONSIBLEUSEREXTENSIONID 
    , sum(Duration) as Talk_Time_seconds
    , round(sum(Duration) / 60,2) as Talk_Time_minutes
    , sum(if(LEGTYPE1 = 1,1,0)) as Outbound   
    , sum(if(LEGTYPE1 = 2,1,0)) as Inbound
    , sum(if(Answered = 1,0,1)) as Miss_Calls
    , count(DISTINCT b.NOTABLECALLID) as Calls
    , NOW()
    , curdate() 
FROM cdrdb.session a
LEFT JOIN cdrdb.callsummary b
     ON a.NOTABLECALLID = b.NOTABLECALLID
LEFT join cdrdb.mxuser c
     ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
WHERE b.ts >= curdate()
AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
group by c.extension
ON duplicate key update Total_Talk_Time_seconds =values(Talk_Time_seconds), 
    Total_Talk_Time_minutes =values(Talk_Time_minutes), 
    Total_Outbound = values(Outbound), 
    Total_Inbound = values(Inbound), 
    Missed_calls = values(Miss_Calls), 
    Total_Calls = values(Calls), 
    Time_of_report = values(NOW());