2016-09-13 4 views
0

Следующий код дает мне несколько строк, так как может быть более одного 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 
+0

http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows?rq=1 – apomene

+0

Использование раздела By –

ответ

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 
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 
0

Может попробовать с MIN (Cust_Edit_Log.Edit_Timestamp)

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 
Смежные вопросы