Subject RE: [firebird-support] 1 a n relationship with GROUP BY conditioned on master - Email found in subject
Author Leyne, Sean
Fabio,

> I have 2 tables
> A
> fields a1, a2, a3
>
> B
> Fields b1 ,b2, b3, b4
>
> I need to create a SQL retrieve sum(b3) and aggregate on b2 and filter on a2
> where a1=b1

SELECT
B2
SUM(B3)
FROM
B
JOIN A ON A1 = B1
WHERE
A2 = :Target
GROUP BY 1

OR

SELECT
B2
SUM(B3)
FROM (
SELECT A1
FROM A
WHERE
A2 = :Target
) T
JOIN B ON B1 = T.A1
GROUP BY 1