如何删除查询中的重复条目?

时间:2016-09-13 11:32:09

标签: sql sql-server

以下代码为我提供了多行,因为每个警报帐户可以有多个Cust_Edit_Log.Edit_Timestamp。没有其他方法可以发生重复。我如何只使用最早的Cust_Edit_Log.Edit_Timestamp日期得到结果?提前感谢您提供的任何帮助。

Select
AR_Customer.Customer_Number As 'Customer_Number',
AR_Customer.Customer_Name As 'Customer_Name',
AR_Customer_System.Alarm_Account As 'Alarm_Account',
AR_Customer_Site.Address_1 As 'Site_Address_1',
Cust_Edit_Log.UserComments As 'Edit_Log_Cust_User_Comments',
Cust_Edit_Log.Edit_Timestamp As 'Edit_Log_Cust_Timestamp',
Cust_Edit_Log.UserCode As 'Edit_Log_Cust_User'
From
AR_Customer
Inner JOIN AR_Customer_Site On AR_Customer.Customer_Id = AR_Customer_Site.Customer_Id
Left Outer JOIN AR_Customer_System On AR_Customer_Site.Customer_Site_Id = AR_Customer_System.Customer_Site_Id
Left Outer Join CQB_Log_Parse Cust_Edit_Log on AR_Customer.Customer_Id = Cust_Edit_Log.Customer_Id
Where
AR_Customer.Customer_Id <> 1 And
(AR_Customer_System.Alarm_Account Like 'IN%' And
Cust_Edit_Log.UserComments Like 'Edited Customer System IN%')
Order By
AR_Customer.Customer_Number ASC

4 个答案:

答案 0 :(得分:0)

使用 Partition BY:

SELECT
    X.*
FROM
(
    Select
    AR_Customer.Customer_Number As 'Customer_Number',
    AR_Customer.Customer_Name As 'Customer_Name',
    AR_Customer_System.Alarm_Account As 'Alarm_Account',
    AR_Customer_Site.Address_1 As 'Site_Address_1',
    Cust_Edit_Log.UserComments As 'Edit_Log_Cust_User_Comments',
    Cust_Edit_Log.Edit_Timestamp As 'Edit_Log_Cust_Timestamp',
    Cust_Edit_Log.UserCode As 'Edit_Log_Cust_User',
    ROW_NUMBER() OVER(Partition BY AR_Customer_System.Alarm_Account,Cust_Edit_Log.Edit_Timestamp ORDER BY AR_Customer_System.Alarm_Account) AS PartNO
    From
    AR_Customer
    Inner JOIN AR_Customer_Site On AR_Customer.Customer_Id = AR_Customer_Site.Customer_Id
    Left Outer JOIN AR_Customer_System On AR_Customer_Site.Customer_Site_Id = AR_Customer_System.Customer_Site_Id
    Left Outer Join CQB_Log_Parse Cust_Edit_Log on AR_Customer.Customer_Id = Cust_Edit_Log.Customer_Id
    Where
    AR_Customer.Customer_Id <> 1 And
    (AR_Customer_System.Alarm_Account Like 'IN%' And
    Cust_Edit_Log.UserComments Like 'Edited Customer System IN%')
)X
WHERE X.PartNo=1
Order By X.Customer_Number ASC

答案 1 :(得分:0)

一种方法使用row_number()

Left Outer Join
(select lp.*,
        row_number() over (partition by lp.Customer_Id
                           order by Edit_Timestamp asc
                          ) as seqnum
 from CQB_Log_Parse lp
) Cust_Edit_Log
on AR_Customer.Customer_Id = Cust_Edit_Log.Customer_Id and
   seqnum = 1

答案 2 :(得分:0)

也许尝试使用MIN(Cust_Edit_Log.Edit_Timestamp)

答案 3 :(得分:0)

您可以尝试使用如下:

;with cte as (
Select
AR_Customer.Customer_Number As 'Customer_Number',
AR_Customer.Customer_Name As 'Customer_Name',
AR_Customer_System.Alarm_Account As 'Alarm_Account',
AR_Customer_Site.Address_1 As 'Site_Address_1',
Cust_Edit_Log.UserComments As 'Edit_Log_Cust_User_Comments',
Cust_Edit_Log.Edit_Timestamp As 'Edit_Log_Cust_Timestamp',
Cust_Edit_Log.UserCode As 'Edit_Log_Cust_User'
,row_number() over(partition by AR_Customer.Customer_Number order by Cust_Edit_Log.Edit_Timestamp) as rownum
From
AR_Customer
Inner JOIN AR_Customer_Site On AR_Customer.Customer_Id = AR_Customer_Site.Customer_Id
Left Outer JOIN AR_Customer_System On AR_Customer_Site.Customer_Site_Id = AR_Customer_System.Customer_Site_Id
Left Outer Join CQB_Log_Parse Cust_Edit_Log on AR_Customer.Customer_Id = Cust_Edit_Log.Customer_Id
Where
AR_Customer.Customer_Id <> 1 And
(AR_Customer_System.Alarm_Account Like 'IN%' And
Cust_Edit_Log.UserComments Like 'Edited Customer System IN%')
--Order By
--AR_Customer.Customer_Number ASC
)
select * from cte where rownum = 1 
order by AR_Customer.Customer_Number ASC