Search code examples
sqlt-sqlgroup-by

SQL Group By function combined with SUM


I'm getting the error

Column 'Nordlux UK$Item.Description' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

when trying to run the SQL statement.

If I remove the SUM function and Group by it works, but I would like to have all the result grouped by my Item number. What have I done wrong in my code?

SELECT
    [Nordlux UK$Item].No_,
    [Nordlux UK$Item].[Description],
    SUM([Nordlux UK$Item Ledger Entry].[Quantity]) AS Int
FROM [Nordlux UK$Item]
JOIN [Nordlux UK$Sales Price]
    ON [Nordlux UK$Item].[No_] = [Nordlux UK$Sales Price].[Item No_]
JOIN [Nordlux UK$Item Ledger Entry]
    ON [Nordlux UK$Item].[No_] = [Nordlux UK$Item Ledger Entry].[Item No_]
WHERE [Nordlux UK$Sales Price].[Sales Code] = 'DUN02'
GROUP BY [Nordlux UK$Item].No_

Solution

  • You need to add [Nordlux UK$Item].[Description] in group by clause - as each column of selection list except aggregated function should be group by clause

    SELECT
        [Nordlux UK$Item].No_,
        [Nordlux UK$Item].[Description],
        SUM([Nordlux UK$Item Ledger Entry].[Quantity]) AS Int
    FROM [Nordlux UK$Item]
    JOIN [Nordlux UK$Sales Price]
        ON [Nordlux UK$Item].[No_] = [Nordlux UK$Sales Price].[Item No_]
    JOIN [Nordlux UK$Item Ledger Entry]
        ON [Nordlux UK$Item].[No_] = [Nordlux UK$Item Ledger Entry].[Item No_]
    WHERE [Nordlux UK$Sales Price].[Sales Code] = 'DUN02'
    GROUP BY [Nordlux UK$Item].No_, [Nordlux UK$Item].[Description]