确保只显示不同的产品 ID
Ensuring Only Distinct Product Id's are shown
考虑从此查询生成的以下部分结果集(源自在 ssms 中创建视图)。
SELECT DISTINCT TOP (100) PERCENT
dbo.LandingHeaders.VesselId, dbo.LandingDetails.ProductId,
SUM(dbo.LandingDetails.Quantity) AS Quantity,
dbo.LandingDetails.UnitPrice
FROM
dbo.LandingDetails
INNER JOIN
dbo.LandingHeaders ON dbo.LandingDetails.LandingId = dbo.LandingHeaders.LandingId
INNER JOIN
dbo.Vessels ON dbo.LandingHeaders.VesselId = dbo.Vessels.VesselId
GROUP BY
dbo.LandingHeaders.VesselId, dbo.LandingDetails.UnitPrice,
dbo.LandingDetails.ProductId, dbo.LandingHeaders.LandingDate1
HAVING
(dbo.LandingHeaders.LandingDate1 BETWEEN CONVERT(DATETIME, '2016-01-06 00:00:00', 102)
AND CONVERT(DATETIME, '2016-01-13 00:00:00', 102))
ORDER BY
dbo.LandingHeaders.VesselId, dbo.LandingDetails.ProductId
我只想 return 获取不同的 ProductId 及其相关数量的总和。
编辑
例如,在那些容器 ID 为 4 的条目中,有三行 ProductId 为 22。理想情况下,我希望 return 以下单行,
4 22 19.1 0.4
到目前为止,我得到的最接近的如下。
SELECT DISTINCT
ld.ProductId,
ROUND(SUM(ld.Quantity), 2) AS Quantity,
ld.UnitPrice, lh.VesselId
FROM
LandingDetails ld
JOIN
LandingHeaders lh ON ld.LandingId = lh.LandingId
GROUP BY
ld.ProductId, ld.UnitPrice, lh.VesselId
WHERE
lh.LandingDate1 BETWEEN '20160106' AND '20160113'
这显然还不够接近。欢迎提出建议。
您不需要 DISTINCT
,GROUP BY
应该为您完成这项工作。
并且WHERE
在GROUP BY
之前
SELECT DISTINCT ld.ProductId,
ROUND(SUM(ld.Quantity),2) AS Quantity,
ld.UnitPrice,
lh.VesselId
FROM LandingDetails ld
JOIN LandingHeaders lh
ON ld.LandingId = lh.LandingId
WHERE lh.LandingDate1 BETWEEN '20160106' AND '20160113'
GROUP BY ld.ProductId, ld.UnitPrice, lh.VesselId
考虑从此查询生成的以下部分结果集(源自在 ssms 中创建视图)。
SELECT DISTINCT TOP (100) PERCENT
dbo.LandingHeaders.VesselId, dbo.LandingDetails.ProductId,
SUM(dbo.LandingDetails.Quantity) AS Quantity,
dbo.LandingDetails.UnitPrice
FROM
dbo.LandingDetails
INNER JOIN
dbo.LandingHeaders ON dbo.LandingDetails.LandingId = dbo.LandingHeaders.LandingId
INNER JOIN
dbo.Vessels ON dbo.LandingHeaders.VesselId = dbo.Vessels.VesselId
GROUP BY
dbo.LandingHeaders.VesselId, dbo.LandingDetails.UnitPrice,
dbo.LandingDetails.ProductId, dbo.LandingHeaders.LandingDate1
HAVING
(dbo.LandingHeaders.LandingDate1 BETWEEN CONVERT(DATETIME, '2016-01-06 00:00:00', 102)
AND CONVERT(DATETIME, '2016-01-13 00:00:00', 102))
ORDER BY
dbo.LandingHeaders.VesselId, dbo.LandingDetails.ProductId
我只想 return 获取不同的 ProductId 及其相关数量的总和。
编辑
例如,在那些容器 ID 为 4 的条目中,有三行 ProductId 为 22。理想情况下,我希望 return 以下单行,
4 22 19.1 0.4
到目前为止,我得到的最接近的如下。
SELECT DISTINCT
ld.ProductId,
ROUND(SUM(ld.Quantity), 2) AS Quantity,
ld.UnitPrice, lh.VesselId
FROM
LandingDetails ld
JOIN
LandingHeaders lh ON ld.LandingId = lh.LandingId
GROUP BY
ld.ProductId, ld.UnitPrice, lh.VesselId
WHERE
lh.LandingDate1 BETWEEN '20160106' AND '20160113'
这显然还不够接近。欢迎提出建议。
您不需要 DISTINCT
,GROUP BY
应该为您完成这项工作。
并且WHERE
在GROUP BY
SELECT DISTINCT ld.ProductId,
ROUND(SUM(ld.Quantity),2) AS Quantity,
ld.UnitPrice,
lh.VesselId
FROM LandingDetails ld
JOIN LandingHeaders lh
ON ld.LandingId = lh.LandingId
WHERE lh.LandingDate1 BETWEEN '20160106' AND '20160113'
GROUP BY ld.ProductId, ld.UnitPrice, lh.VesselId