如何使用内部连接sql消除sql中的重复记录

时间:2016-04-19 10:41:58

标签: c# sql sql-server

通过帖子在这里,但我仍然无法做同样的事情。请任何人有一些想法请与我分享。

    SELECT DISTINCT 
    TR.Id as Travel_RequestId,
    TREP.FirstName+' '+TREP.LastName as [Traveler Name],
    TR.EmployeeNo,
    TR.RequestNO,
    TravelType=(case TR.IsOneWay
    when '1' THEN 'OW'
    ELSE 'RT'
    END),
    TRPD.SWONCode,
    cast(TRD.GESSReceivedDate as date) as GESSReceivedDate,
    TRD.Deal,
    TRD.AirLineCode,
    cast(TRD.DepartureDate as date) as Departure_Date,
    cast(TRD.TentativeReturnDate as date) as Tentative_Return_Date,
    cast(TRTD.IssuanceDate as Date) as Tcket_IssuanceDate,
    TRTD.DeskName,
    (select Description from AirlineName where Code=TRD.AirlineNameCode) as Airline,
    (select Description from Currency where Code=TRID.CurrencyTypeCode) as CurrencyType,
    case when (isnumeric(TRTD.BaseFare) & isnumeric(TRTD.ServiceFee)) =1 then 
    (convert(float, TRTD.BaseFare)+convert(float, TRTD.ServiceFee))
    else 
    null 
    end as TicketAmount,
    TRTD.Number As [Ticket Number],
    TRTD.AgentName,
    cast(TRID.IssueDate as date ) As InvoiceDate,
    cast(TRID.CreditNoteDate as date) as CreditNoteDate,
    TRID.CreditNoteNumber,
    TRID.CreditNoteAmount,
    TRID.AmountPayable,
    TRTD.TotalFare,
    TRTD.DeskName,
    TRID.InvoiceAmount,
    TRID.InvoiceNumber,
    TRID.CurrencyTypeCode,
    TRID.AdcolAmount,
    TRID.ChqNo,
    TM.Description as Travel_Mode,

    TRID.Statement,

    TRD.Sector As Itineary,
    TRD.ServiceClass As ClassOfTravel

    from TravelRequest TR LEFT JOIN(SELECT mt.*    
    FROM TravelRequestDetails mt INNER JOIN
(
    SELECT travelrequestId, MIN(DepartureDate) DepartureDate
    FROM TravelRequestDetails
    GROUP BY travelrequestId
) t ON mt.travelrequestId = t.travelrequestId AND mt.DepartureDate = t.DepartureDate)TRD ON TRD.TravelRequestId =TR.Id 

     left join TravelRequestEmployeeProfile TREP 
    on TREP.TravelRequestId=TRD.TravelRequestId
    left join TravelRequestProjectDetails TRPD 
    on TRPD.TravelRequestId=TRD.TravelRequestId
    left join TravelMode TM 
    on TM.Code=TRPD.TravelModeCode 
    left join TravelRequestTicketDetails TRTD 
    on TRTD.TravelRequestId=TRD.TravelRequestId
    left join TravelRequestInvoiceDetails TRID
    on TRID.TravelRequestId=TRD.TravelRequestId
    where TR.Status='CL' and TR.RequestType='BT'
     and TR.UpdatedOn Between DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '18:01'
             AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '18:00' order by [Traveler Name]

我在下面的o / p中需要删除空记录: enter image description here

下面是屏幕截图,我需要根据突出显示的行

中的请求no删除重复项

3 个答案:

答案 0 :(得分:0)

好的,所以SQL正在做你要告诉它的事情并删除重复的数据行。但是,您希望它执行的操作仅基于ID返回重复项。

您必须检查这是否适用于您的数据,但在外部选择中,您可以将非ID的所有内容更改为聚合。如果要在所有其他列上执行MAX,然后在ID字段的GROUP BY上执行MAX,则每个ID最终会有一行,其余列的最高值也会最多。在提供的数据中,这将为您提供您所追求的内容,但请再次检查您的完整数据集,以确定在推送代码之前是否为您提供了正确的结果。

答案 1 :(得分:0)

如果每个Travel_RequestId只有记录,您可以使用group by

with cte as 
( SELECT DISTINCT 
    TR.Id as Travel_RequestId,
    TREP.FirstName+' '+TREP.LastName as [Traveler Name],
    TR.EmployeeNo,
    TR.RequestNO,
    TravelType=(case TR.IsOneWay
    when '1' THEN 'OW'
    ELSE 'RT'
    END),
    TRPD.SWONCode,
    cast(TRD.GESSReceivedDate as date) as GESSReceivedDate,
    TRD.Deal,
    TRD.AirLineCode,
    cast(TRD.DepartureDate as date) as Departure_Date,
    cast(TRD.TentativeReturnDate as date) as Tentative_Return_Date,
    cast(TRTD.IssuanceDate as Date) as Tcket_IssuanceDate,
    TRTD.DeskName,
    (select Description from AirlineName where Code=TRD.AirlineNameCode) as Airline,
    (select Description from Currency where Code=TRID.CurrencyTypeCode) as CurrencyType,
    case when (isnumeric(TRTD.BaseFare) & isnumeric(TRTD.ServiceFee)) =1 then 
    (convert(float, TRTD.BaseFare)+convert(float, TRTD.ServiceFee))
    else 
    null 
    end as TicketAmount,
    TRTD.Number As [Ticket Number],
    TRTD.AgentName,
    cast(TRID.IssueDate as date ) As InvoiceDate,
    cast(TRID.CreditNoteDate as date) as CreditNoteDate,
    TRID.CreditNoteNumber,
    TRID.CreditNoteAmount,
    TRID.AmountPayable,
    TRTD.TotalFare,
    TRTD.DeskName,
    TRID.InvoiceAmount,
    TRID.InvoiceNumber,
    TRID.CurrencyTypeCode,
    TRID.AdcolAmount,
    TRID.ChqNo,
    TM.Description as Travel_Mode,

    TRID.Statement,

    TRD.Sector As Itineary,
    TRD.ServiceClass As ClassOfTravel

    from TravelRequest TR LEFT JOIN(SELECT mt.*    
    FROM TravelRequestDetails mt INNER JOIN
(
    SELECT travelrequestId, MIN(DepartureDate) DepartureDate
    FROM TravelRequestDetails
    GROUP BY travelrequestId
) t ON mt.travelrequestId = t.travelrequestId AND mt.DepartureDate = t.DepartureDate)TRD ON TRD.TravelRequestId =TR.Id 

     left join TravelRequestEmployeeProfile TREP 
    on TREP.TravelRequestId=TRD.TravelRequestId
    left join TravelRequestProjectDetails TRPD 
    on TRPD.TravelRequestId=TRD.TravelRequestId
    left join TravelMode TM 
    on TM.Code=TRPD.TravelModeCode 
    left join TravelRequestTicketDetails TRTD 
    on TRTD.TravelRequestId=TRD.TravelRequestId
    left join TravelRequestInvoiceDetails TRID
    on TRID.TravelRequestId=TRD.TravelRequestId
    where TR.Status='CL' and TR.RequestType='BT'
     and TR.UpdatedOn Between DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '18:01'
             AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '18:00'
)

select Travel_RequestId, max ([Traveler Name]) as [Traveler Name], max (EmployeeNo) as EmployeeNo, max (RequestNO) as RequestNO, 
max (TravelType) as TravelType, max(SWONCode) as SWONCode /* and other columns */
from cte

答案 2 :(得分:0)

我认为您可以在Partition Byrow_number()

的帮助下完成此操作

示例:

select row_number() over (partition by customerId order by orderId) as rowNum, customerId,Name,number over (partition by customerId order by orderId) from Orders where rowNum=1

参考Group by vs partition by

已修改

在您的查询中

select ROW_NUMBER() over (partition by TR.RequestNOorder by TRD.DepartureDate) as rowNum, TR.RequestNOorder,min(TR.DepartureDate) over (partition by TR.RequestNOorder) as DepartureDate from TravelRequestDetails where rowNum=1