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