In the below SQL Statement, should I be using DISTINCT as I have a Group By in my Where Clause? Thoughts?
SELECT [OrderUser].OrderUserId, ISNULL(SUM(total.FileSize), 0), ISNULL(SUM(total.CompressedFileSize), 0)
FROM
(
SELECT DISTINCT ProductSize.OrderUserId, ProductSize.FileInfoId,
CAST(ProductSize.FileSize AS BIGINT) AS FileSize,
CAST(ProductSize.CompressedFileSize AS BIGINT) AS CompressedFileSize
FROM ProductSize WITH (NOLOCK)
INNER JOIN [Version] ON ProductSize.VersionId = [Version].VersionId
) AS total RIGHT OUTER JOIN [OrderUser] WITH (NOLOCK) ON total.OrderUserId = [OrderUser].OrderUserId
WHERE NOT ([OrderUser].isCustomer = 1 AND [OrderUser].isEndOrderUser = 0 OR [OrderUser].isLocation = 1)
AND [OrderUser].OrderUserId = 1
GROUP BY [OrderUser].OrderUserId
Since the question wasn’t written as clearly as possible, I am assuming you are asking if the
distinctis extraneous due to thegroup bythat exists.If the inner query returns multiple records due to the
inner join, you will need adistinctthere. Otherwise theright outer joinwill join on more records than you intend and things likeSUM()will not return the same values.So
DISTINCTis not extraneous.