Subject | Re: [firebird-support] Master-detail group by problem |
---|---|
Author | Svein Erling Tysvaer |
Post date | 2009-04-19T18:07:36Z |
If I understand the question, the answer is quite simple and there's no
need for any stored procedure:
SELECT <whatever you want from M and D>
FROM MASTER M
JOIN DETAIL D ON M.MasterID = D.MasterID
LEFT JOIN DETAIL D2 ON D.MasterID = D2.MasterID
AND D.SecondMasterID <> D2.SecondMasterID
WHERE D2.DetailID IS NULL
A similar query can also be used, in this example I assume that you're
only interested in information from the master, if not, just replace the
outer EXISTS with a JOIN as above:
SELECT <whatever you want from M>
FROM MASTER M
WHERE EXISTS(SELECT * FROM DETAIL D WHERE M.MasterID = D.MasterID
WHERE NOT EXISTS (SELECT * FROM DETAIL D2
WHERE D.MasterID = D2.MasterID
AND D.SecondMasterID <> D2.SecondMasterID))
HTH,
Set
Harriv wrote:
need for any stored procedure:
SELECT <whatever you want from M and D>
FROM MASTER M
JOIN DETAIL D ON M.MasterID = D.MasterID
LEFT JOIN DETAIL D2 ON D.MasterID = D2.MasterID
AND D.SecondMasterID <> D2.SecondMasterID
WHERE D2.DetailID IS NULL
A similar query can also be used, in this example I assume that you're
only interested in information from the master, if not, just replace the
outer EXISTS with a JOIN as above:
SELECT <whatever you want from M>
FROM MASTER M
WHERE EXISTS(SELECT * FROM DETAIL D WHERE M.MasterID = D.MasterID
WHERE NOT EXISTS (SELECT * FROM DETAIL D2
WHERE D.MasterID = D2.MasterID
AND D.SecondMasterID <> D2.SecondMasterID))
HTH,
Set
Harriv wrote:
> Hi,
> I've master - detail tables, and detail has also FK to another master
> table. Basically
>
> MASTER
> ========
> MasterID integer
>
>
> DETAIL
> =======
> DetailID integer
> MasterID integer
> SecondMasterID integer
>
> I'm doing query "Select ... from master where Master.MasterID =
> Detail.MasterID"
>
> Now I want to limit the result set for the query so that it contains only
> Master rows where all the detail rows have same SecondMasterID for that
> specific MasterID, how do I limit that in Firebird 1.5?
>
> Something like "Select .. from master where Master.MasterID =
> Detail.MasterID and (select number of rows from detail where Master.MasterID
> = Detail.MasterID) = 1" in "pseudo SQL".