Subject Re: [firebird-support] Is it possible calculate result of aggregates?
Author setysvar
Den 22.01.2016 11:51, skrev Luigi Siciliano luigisic@...
[firebird-support]:
> How can I do this query:
>
> Select
> A,
> B,
> SUM(A + B) as C,
> C * A as D ----->>>>> FB result on "C not Exists" error!
> from
> table
> group by
> A,
> B
>
> Of course, I can do this: "SUM(A + B) * A as D" to perform my query but,
> I wish to learn if is possible the other one (C * A as D)
>
> Thanks.
WITH TMP(A, B, C) AS
(SELECT A, B, SUM(A+B)
FROM TABLE
GROUP BY A, B)
SELECT A, B, C, C*A as D
FROM TMP

Note that with two identical rows

A B
2 2
2 2

The result will be

A B C D
2 2 8 16

HTH,
Set