Skip to content
Advertisement

How to use group by in sql? (I need sequelize)

I have

SELECT [id], [company_number], [country_code] 
FROM [core].[company] 
WHERE [core].[company].[id] IN (N'DK-10001560') 
GROUP BY [country_code]

But it fails with this error:

Column 'core.company.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Works fine without GROUP BY

Advertisement

Answer

The GROUP BY statement groups rows that have the same values into summary rows, in your case, it is the country_code.

It is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

That means, for expl : you want to count how many company_number per country_code

So, what you can do is :

    SELECT id, company_number, count(country_code )
    FROM core.company 
    WHERE core.company.id IN (N'DK-10001560') 
    GROUP BY id, company_number

Hope this helps 😉

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement