SQL查询产生重复的行,我不明白为什么

Cod*_*ome 2 sql duplicate-data

我的查询总是产生重复的结果.我最好如何使用数据库> 100万行来解决此查询问题.

Select segstart
    ,segment
    ,callid
    ,Interval
    ,dialed_num
    ,FiscalMonthYear
    ,SegStart_Date
    ,row_date
    ,Name
    ,Xferto
    ,TransferType
    ,Agent
    ,Sup
    ,Manager
    ,'MyCenter' = Case Center
When 'Livermore Call Center' Then 'LCC'
When 'Natomas Call Center' Then 'NCC'
When 'Concord Call Center' Then 'CCC'
When 'Virtual Call Center' Then 'VCC'
When 'Morgan Hill Call Center' Then 'MHCC'
Else Center
End
    ,Xferfrom
    ,talktime
    ,ANDREWSTABLE.transferred
    ,ANDREWSTABLE.disposition
    ,dispsplit
    ,callid
    ,hsplit.starttime
    ,CASE
    WHEN hsplit.callsoffered > 0 
    THEN (CAST(hsplit.acceptable as DECIMAL)/hsplit.callsoffered)*100
    ELSE '0'
    END AS 'Service Level'
    ,hsplit.callsoffered
    ,hsplit.acceptable
FROM
(
Select segstart,
    100*DATEPART(HOUR, segstart) + 30*(DATEPART(MINUTE, segstart)/30) as Interval,
    FiscalMonthYear,
    SegStart_Date,
    dialed_num,
    callid,
    Name,
    t.Queue AS 'Xferto',
    TransferType,
    RepLName+', '+RepFName AS Agent,
    SupLName+', '+SupFName AS Sup,
    MgrLName+', '+MgrFName AS Manager,
    q.Center,
    q.Queue AS 'Xferfrom',
    e.anslogin,
    e.origlogin,
    t.Extension,
    transferred,
    disposition,
    talktime,
    dispsplit,
    segment
From CMS_ECH.dbo.CaliforniaECH e

INNER JOIN Cal_RemReporting.dbo.TransferVDNs t on e.dialed_num = t.Extension
INNER JOIN InfoQuest.dbo.IQ_Employee_Profiles_v3_AvayaId q on e.origlogin = q.AvayaID
INNER JOIN Cal_RemReporting.dbo.udFiscalMonthTable f on e.SegStart_Date = f.Tdate

Where SegStart_Date between getdate()-90 and getdate()-1
    And q.Center not in ('Collections Center',
                         'Cable Store',
                         'Business Services Center',
                         'Escalations')
    And SegStart_Date between RepToSup_StartDate and RepToSup_EndDate
    And SegStart_Date between SupToMgr_StartDate and SupToMgr_EndDate
    And SegStart_Date between Avaya_StartDate and Avaya_EndDate
    And SegStart_Date between RepQueue_StartDate and RepQueue_EndDate
    AND (e.transferred like '1'
    OR e.disposition like '4') order by segstart
) AS ANDREWSTABLE

--Left Join CMS_ECH.dbo.hsplit hsplit on hsplit.starttime = ANDREWSTABLE.Interval and hsplit.row_date = ANDREWSTABLE.SegStart_Date and ANDREWSTABLE.dispsplit = hsplit.split
Run Code Online (Sandbox Code Playgroud)

APC*_*APC 6

有两种可能性:

  1. 系统中有多条记录似乎会在结果集中产生重复的行,因为您的投影不会选择足够的列来区分它们,或者您的where子句不会将它们过滤掉.
  2. 您的连接产生虚假重复,因为ON子句不完整.

这些都只能由具有必要领域知识水平的人来解决.所以我们不打算为你解决这个问题.抱歉.

您需要做的是将一些重复的结果与一些非重复的结果共同显示,并发现第一组的共同点,它也区别于第二组.

我并不是说它很容易,特别是有数百万行.但如果这很容易就不值得做.

  • @CodingAwesome - 当ON子句没有标识唯一键时,它们将产生交叉连接.如果其中一个表具有复合键,并且我们未指定其所有列,或者我们连接到不是唯一键的列,则可能会发生这种情况. (4认同)