简体   繁体   中英

Multiple joins error in group by when I use SUM function

I'm getting this error:

Column 'MDR.dbo.nav_BG$Location.Name' is invalid in the select list because 
it is not contained in either an aggregate function or the GROUP BY clause.

He wants from me to add all my fields in group by but this is incorrect.

DECLARE @date datetime2 = CURRENT_TIMESTAMP;
SELECT   
[Source Document ID]        AS Source
,Location.Name              AS Address
,[Item No_]                 AS No
,[Document No_]             AS Document
,Item.[No_ 2]               AS No2
,MN.Name                    AS Brand
,SUM([Quantity Requested])  AS Qty,
CASE WHEN qty > 20 THEN  'No' ELSE 'Yes'  END AS Priority
,[Created On]               AS Date


FROM [MDR].[dbo].[nav_BG$AWHM_Document_Line] AS DocLine
JOIN [MDR].[dbo].[nav_BG$Item] AS Item ON DocLine.[Item No_] = Item.[No_]
JOIN [MDR].[dbo].[nav_BG$Location] AS Location ON DocLine.[Source Document ID] = Location.Code
JOIN [MDR].[dbo].[nav_BG$Manufacturer] AS MN ON Item.[Manufacturer Code] = MN.Code

WHERE [Location Code] = 'XXX'
AND   [Document Type] = 'XXX'
AND   [From Zone Code] = 'XXX'
AND   (DATEPART(yy, [Created On]) = YEAR(@date)
AND    DATEPART(mm, [Created On]) = MONTH(@date)
AND    DATEPART(dd, [Created On]) = DAY(@date))
GROUP BY [Source Document ID]
ORDER BY [Created On]

Expected result would be the SUM([Quantity Requested]) group by [Source Document ID] .

How to use only one group by [Source Document ID] ?

If you're going to use an aggregate function, you normally need to specify all your non-aggregated columns (like Location.Name ) in the GROUP BY clause. Otherwise, you can't return them in your query.

If you want to return non-aggregated fields and also include an aggregate, you need to use a window function:

SUM([Quantity Requested]) OVER(PARTITION BY [Source Document ID])

If your DBMS supports it, it would look like:

SELECT   
[Source Document ID]        AS Source
,Location.Name              AS Address
,[Item No_]                 AS No
,[Document No_]             AS Document
,Item.[No_ 2]               AS No2
,MN.Name                    AS Brand
,SUM([Quantity Requested]) OVER(PARTITION BY [Source Document ID])  AS Qty,
CASE WHEN qty > 20 THEN  'No' ELSE 'Yes'  END AS Priority
,[Created On]               AS Date

FROM [MDR].[dbo].[nav_BG$AWHM_Document_Line] AS DocLine
JOIN [MDR].[dbo].[nav_BG$Item] AS Item ON DocLine.[Item No_] = Item.[No_]
JOIN [MDR].[dbo].[nav_BG$Location] AS Location ON DocLine.[Source Document ID] = Location.Code
JOIN [MDR].[dbo].[nav_BG$Manufacturer] AS MN ON Item.[Manufacturer Code] = MN.Code

WHERE [Location Code] = 'XXX'
AND   [Document Type] = 'XXX'
AND   [From Zone Code] = 'XXX'
AND   (DATEPART(yy, [Created On]) = YEAR(@date)
AND    DATEPART(mm, [Created On]) = MONTH(@date)
AND    DATEPART(dd, [Created On]) = DAY(@date))
-- GROUP BY [Source Document ID]
ORDER BY [Created On]

This will let you return the detail rows along with the SUM() calculated over each [Source Document ID] group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM