Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_
like image 538
Morten Lau Torp Nielsen Avatar asked Nov 18 '25 08:11

Morten Lau Torp Nielsen


1 Answers

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]
like image 124
Fahmi Avatar answered Nov 20 '25 21:11

Fahmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!