i have a table with this columns:"id,Nationalcode,Ccode,Ocode,Adate,..." now i want do group by on Nationalcode, select id,Kasset from allocate group by Kasset
but for each Nationalcode there are several record hence hit to following error: Column 'allocate.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
what is correct command?
It's clear enough from the error message
Column 'allocate.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
that while having several Allocate.Id
values, eg
1, 2, 3
you have to figure out one with a help of aggregate function :
1, 2, 3 -> SUM() -> 6
1, 2, 3 -> AVG() -> 2
1, 2, 3 -> MIN() -> 1
1, 2, 3 -> MAX() -> 3
1, 2, 3 -> COUNT() -> 3
etc. The query should be
select Max(Id), --TODO: Put the right function here
Kasset
from Allocate
group by Kasset
Edit : let's try finding out these aggregate functions (at least some of them), according to the example (see comments below)
id | Nationalcode | Ccode | Ocode | Kasset | Adat
-------------------------------------------------------
1 | 547 | 1910 | 1 | 4444 | 1995/09/27
2 | 546 | 1910 | 1 | 2222 | 1995/12/14
1 | 546 | 1910 | 1 | 4444 | 1995/01/01
The desired result is
1 | 546 | 1910 | 1 | 4444 | 1995/09/27
2 | 546 | 1910 | 1 | 2222 | 1995/12/14
the query can be
select Max(Id), -- ? Many options, Max among them
Min(Nationalcode), -- since 546, 547 -> 546
Max(Ccode), -- ? Many options, Max among them
Max(Ocode), -- ? Many options, Max among them
Kasset, -- ? Many options, Max among them
Max(Adat) -- 1995/09/27, 1995/01/01 -> 1995/09/27
from Allocate
group by Kasset
GROUP BY clause is always used with aggregate function (ie SUM, COUNT, MIN, MAX, or AVG functions)
Try this:
select count(id) [Count], Kasset from allocate group by Kasset
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.