Subject Re: [firebird-support] can i aggregate data to get an unique item?
Author Markus Ostenried
On Mon, Apr 11, 2011 at 17:00, ndirondello marco <korkless@...> wrote:
> hi all, i have this 2 tables:
>
> Table_A
> {
>   ID Bigint
>   Name VarChar
> }
>
> Table_B
> {
>   ID BigInt
>   TARGET_ID fk to table A
> }
>
> i need to get this result:
>
> select count(b.Id), a.Name
> from Table_A a
> join Table_B b
> on a.Id = b.Target_Id
> group by(a.Id)
>
> but it doesn't works becouse a.Name is not an aggregate function, what's the best way to get it?

Hi, AFAIK you need to include all SELECTed (but not aggregated)
columns in the GROUP BY clause. So if A.NAME is unique you could write
GROUP BY A.NAME

If A.NAME is not unique then I think you could
SELECT COUNT(B.ID), A.ID, A.NAME
...
GROUP BY A.ID, A.NAME

Not tested, you need to try it out yourself.

HTH,
Markus