Subject Re: [firebird-support] Conditional grouping?
Author Ivan Prenosil
> I'm probably being dense, but I can't figure out how to do a conditional
> grouping.
> I need to sum quantities based on these columns (GPI, TEE, UNIT_DOSE),
> but also on an additional column (DOSE_SIZE) if one of first set
> (UNIT_DOSE) meets certain criteria.
>
> GROUP BY
> GPI, TEE, UNIT_DOSE, (IF UNIT_DOSE IN ('X','U') DOSE_SIZE)
>
> The only thing I could come up with is to do two queries, union them,
> and then sum that.

SELECT CASE WHEN UNIT_DOSE IN ('X','U') THEN DOSE_SIZE ELSE NULL END, ...
GROUP BY GPI, TEE, UNIT_DOSE, 1

Ivan