2010-12-17 52 views
1

我的SQL查詢連接到多個表,因此它顯示多個結果。我知道SELECT DISTINCT,但其中一個字段('Account.Name')偶爾會有所不同,因此它將此記錄視爲新行。有任何想法嗎?這是我的SQL:SQL幫助 - 返回來自加入的重複項

SELECT DISTINCT 
         d.ID, ISNULL(d.OnHold, 0) AS 'OnHold', d.OnHoldReason AS 'On Hold Reason', d.LegacyID AS 'Consignment#', d.IntConNo AS 'Consignment Ref', 
         CASE WHEN JobType = 'O' THEN 'Outward' ELSE 'Collection' END AS 'Job Type', d.TripDate AS 'Delivery Date', d.DeliveryTown AS 'Delivery Town', 
         d.DeliveryPostcode AS 'Delivery Postcode', d.VehicleReg AS 'Vehicle Reg', d.RequiredCollectionDate AS 'Req. Collection Date', 
         d.VehicleReg AS 'Vehicle Registration', CASE WHEN d .DeliveryStatus = 2 THEN 'OK' END AS 'Delivery Status', d.ItemsignedBy AS 'POD Signed By', 
         d.BaseRate AS 'Base Rate', d.FuelSurcharge AS 'Fuel Surcharge', d.AdditionalCharges AS 'Additional Charges', 
         d.BaseRate + d.FuelSurcharge + d.AdditionalCharges AS 'Value', CASE WHEN InvoiceNumber IS NULL THEN CONVERT(bit, 0) ELSE CONVERT(bit, 1) 
         END AS Tagged, CASE WHEN di.Total = di.Delivered THEN 'Recieved' ELSE 'Not Recieved' END AS 'Items', Account.Name 
FROM   Deliveries AS d LEFT OUTER JOIN 
          (SELECT  Consignment, COUNT(*) AS Total, COUNT(CASE WHEN Status = 2 THEN 1 END) AS Delivered 
          FROM   Items 
          GROUP BY Consignment) AS di ON d.ID = di.Consignment INNER JOIN 
         Account ON d.Customer = Account.InvoiceAccount 
WHERE  (d.InvoiceNumber IS NULL) AND (d.TripDate > 29/11/2010) 
ORDER BY 'Consignment#' 
+2

如果Account.Name不同,那麼它是一個新記錄。我不清楚你想限制什麼。如果您不希望查看Account.Name的所有匹配項,請不要返回它,或者使用子選擇或sproc來生成包含所有Account.Name實例(即連接在一起)的聚合字段。 – Lazarus 2010-12-17 10:37:29

回答

1

最簡單的解決方案是用GROUP BY替換DISTINCT。問題是隻有一個帳戶名稱被返回。

其他的解決方案可能是

  • 創建一個函數返回級聯帳戶名
  • 使用子選擇通過

    SELECT d.ID 
         , ISNULL(d.OnHold, 0) AS 'OnHold' 
         , d.OnHoldReason AS 'On Hold Reason' 
         , d.LegacyID AS 'Consignment#' 
         , d.IntConNo AS 'Consignment Ref' 
         , CASE WHEN JobType = 'O' THEN 'Outward' ELSE 'Collection' END AS 'Job Type' 
         , d.TripDate AS 'Delivery Date' 
         , d.DeliveryTown AS 'Delivery Town' 
         , d.DeliveryPostcode AS 'Delivery Postcode' 
         , d.VehicleReg AS 'Vehicle Reg' 
         , d.RequiredCollectionDate AS 'Req. Collection Date' 
         , d.VehicleReg AS 'Vehicle Registration' 
         , CASE WHEN d .DeliveryStatus = 2 THEN 'OK' END AS 'Delivery Status', d.ItemsignedBy AS 'POD Signed By' 
         , d.BaseRate AS 'Base Rate' 
         , d.FuelSurcharge AS 'Fuel Surcharge' 
         , d.AdditionalCharges AS 'Additional Charges' 
         , d.BaseRate + d.FuelSurcharge + d.AdditionalCharges AS 'Value' 
         , CASE WHEN InvoiceNumber IS NULL THEN CONVERT(bit, 0) ELSE CONVERT(bit, 1) END AS Tagged 
         , CASE WHEN di.Total = di.Delivered THEN 'Recieved' ELSE 'Not Recieved' END AS 'Items' 
         , MIN(Account.Name) 
    FROM Deliveries AS d 
         LEFT OUTER JOIN (
          SELECT Consignment 
            , COUNT(*) AS Total 
            , COUNT(CASE WHEN Status = 2 THEN 1 END) AS Delivered 
          FROM Items 
          GROUP BY 
            Consignment 
         ) AS di ON d.ID = di.Consignment 
         INNER JOIN Account ON d.Customer = Account.InvoiceAccount 
    WHERE (d.InvoiceNumber IS NULL) 
         AND (d.TripDate > 29/11/2010) 
    GROUP BY 
         d.ID 
         , ISNULL(d.OnHold, 0) 
         , d.OnHoldReason 
         , d.LegacyID 
         , d.IntConNo 
         , CASE WHEN JobType = 'O' THEN 'Outward' ELSE 'Collection' END 
         , d.TripDate 
         , d.DeliveryTown 
         , d.DeliveryPostcode 
         , d.VehicleReg 
         , d.RequiredCollectionDate 
         , d.VehicleReg 
         , CASE WHEN d .DeliveryStatus = 2 THEN 'OK' END AS 'Delivery Status', d.ItemsignedBy 
         , d.BaseRate 
         , d.FuelSurcharge 
         , d.AdditionalCharges 
         , d.BaseRate + d.FuelSurcharge + d.AdditionalCharges 
         , CASE WHEN InvoiceNumber IS NULL THEN CONVERT(bit, 0) ELSE CONVERT(bit, 1) END 
         , CASE WHEN di.Total = di.Delivered THEN 'Recieved' ELSE 'Not Recieved' END 
    ORDER BY 
         'Consignment#' 
    
    返回只有一個名字

使用組

+0

感謝哥們,我想我會沿着子選擇路線走下去。 – Chris 2010-12-17 10:49:33