2016-09-13 8 views
0

次のコードは、アラームアカウントごとに1つ以上のCust_Edit_Log.Edit_Timestampが存在する可能性があるため、複数の行を示しています。重複が発生する他の方法はありません。最も早いCust_Edit_Log.Edit_Timestamp日付でのみ結果を取得するにはどうすればよいですか?あなたが提供できるあらゆる助けをありがとうございます。 BY私のクエリで重複したエントリを削除するには?

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

パーティション分割を使用する –

答えて

0

使用パーティション:

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 
関連する問題