如何从我的SQL查询中删除重复的结果

时间:2010-07-17 22:33:54

标签: sql sql-server sql-server-2005

非常感谢您的帮助!

好的,我的查询产生重复结果的部分是:

Left Join CMS_ECH.dbo.hsplit hsplit on hsplit.row_date = ANDREWSTABLE.SegStart_Date 
                                   and hsplit.split = ANDREWSTABLE.dispsplit 
                                   and hsplit.starttime = ANDREWSTABLE.Interval

我认为由于hsplit表没有任何主键,它会产生重复的结果。虽然我不是管理员,但我无法设置主键。我问过,但我想要一个解决方法。

我看过这篇文章是一个解决方案: SQL - How can I remove duplicate rows?

但是,我如何将其应用于我的查询:

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')
) AS ANDREWSTABLE

Left Join CMS_ECH.dbo.hsplit hsplit on hsplit.row_date = ANDREWSTABLE.SegStart_Date and hsplit.split=ANDREWSTABLE.dispsplit and hsplit.starttime = ANDREWSTABLE.Interval

3 个答案:

答案 0 :(得分:4)

也许SELECT DISTINCT...可以为您提供帮助(只需在您的选择后添加DISTINCT)。我不知道你的申请是否足够,但为什么不尝试呢。

答案 1 :(得分:3)

  

我认为由于hsplit表没有任何主键,它会产生重复的结果。虽然我不是管理员,但我无法设置主键。我问过,但我想要一个解决方法。

问题是,根据加入条件CMS_ECH.dbo.hsplitrow_date和{{},split表格中有多行与您要加入的内容相匹配1}})。它不需要管理员 - 您需要查看starttime表中的数据,看看如何获​​得一对一的匹配(假设可能)。

我建议在考虑像GROUP BY或DISTINCT这样的绑定解决方案之前先查看一下......

答案 2 :(得分:1)

使用GROUP BY声明