Subject | Re: query for min and max value in group |
---|---|
Author | harri007et |
Post date | 2004-06-05T16:57:13Z |
--- In firebird-support@yahoogroups.com, "Arno Brinkman"
<firebird@a...> wrote:
Somehow I thought, that subquery with aggregate function inside the
having clause would be very slow - but it was only about twice
slower as SP, so I'm happy with it.
best regards,
Harri
<firebird@a...> wrote:
> Do you meant where the corresponding lower t2.C to the groupis "A", then
> this is maybe the solution :you have
>
> SELECT
> t1.A,
> MIN(t1.B),
> (SELECT t2.C FROM ATable t2 WHERE t2.A = t1.A and
> t2.B = MIN(t1.B))
> FROM
> ATable t1
> GROUP BY
> t1.A
> HAVING
> (SELECT t2.C FROM ATable t2 WHERE t2.A = t1.A and
> t2.B = MIN(t1.B)) = 'A'
>
> SELECT
> t1.A,
> MAX(t1.B),
> (SELECT t2.C FROM ATable t2 WHERE t2.A = t1.A and
> t2.B = MAX(t1.B))
> FROM
> ATable t1
> GROUP BY
> t1.A
> HAVING
> (SELECT t2.C FROM ATable t2 WHERE t2.A = t1.A and
> t2.B = MAX(t1.B)) = 'A'
>
> Although this could indeed be expensive depending on the indexes
> and a SP may be much cheaper. With UNION ALL you can also combindboth
> queries together.Thanks!
>
> Regards,
> Arno Brinkman
> ABVisie
Somehow I thought, that subquery with aggregate function inside the
having clause would be very slow - but it was only about twice
slower as SP, so I'm happy with it.
best regards,
Harri