我的SQL查询连接多个表,因此它显示多个结果。我知道SELECT DISTINCT,但其中一个字段('Account.Name')偶尔会有所不同,因此它将此记录视为新行。有任何想法吗?这是我的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#'
答案 0 :(得分:1)
最简单的解决方案是用GROUP BY替换DISTINCT。该dowside只返回一个帐户名。
其他解决方案可能
使用分组
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#'