Subject | Re: [firebird-support] How to join the result of 2 select statements |
---|---|
Author | Arno Brinkman |
Post date | 2004-06-24T09:49:14Z |
Hi,
SELECT
r.ReservationID,
(SELECT SUM(p.amount) FROM Payment p WHERE
p.ReservationID = r.ReservationID) AS TotalPayment,
(SELECT SUM(d.amount) FROM Debt d WHERE
d.ReservationID = r.ReservationID) AS TotalDebt,
(SELECT SUM(p.amount) FROM Payment p WHERE
p.ReservationID = r.ReservationID) +
(SELECT SUM(d.amount) FROM Debt d WHERE
d.ReservationID = r.ReservationID) AS Total
FROM
Reservation r
GROUP BY
r.ReservationID
You could also put the above first 2 sub-queries in an SP and sum those two
there that would avoid executing 2 times the same sub-query.
Regards,
Arno Brinkman
ABVisie
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Firebird open source database (based on IB-OE) with many SQL-99 features :
http://www.firebirdsql.org
http://www.firebirdsql.info
http://www.fingerbird.de/
http://www.comunidade-firebird.org/
Support list for Interbase and Firebird users :
firebird-support@yahoogroups.com
Nederlandse firebird nieuwsgroep :
news://newsgroups.firebirdsql.info
> I've been stuck getting a query running for a few days now. Clean somebody<snip>
> help me please.
> The problem is this.
SELECT
r.ReservationID,
(SELECT SUM(p.amount) FROM Payment p WHERE
p.ReservationID = r.ReservationID) AS TotalPayment,
(SELECT SUM(d.amount) FROM Debt d WHERE
d.ReservationID = r.ReservationID) AS TotalDebt,
(SELECT SUM(p.amount) FROM Payment p WHERE
p.ReservationID = r.ReservationID) +
(SELECT SUM(d.amount) FROM Debt d WHERE
d.ReservationID = r.ReservationID) AS Total
FROM
Reservation r
GROUP BY
r.ReservationID
You could also put the above first 2 sub-queries in an SP and sum those two
there that would avoid executing 2 times the same sub-query.
Regards,
Arno Brinkman
ABVisie
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Firebird open source database (based on IB-OE) with many SQL-99 features :
http://www.firebirdsql.org
http://www.firebirdsql.info
http://www.fingerbird.de/
http://www.comunidade-firebird.org/
Support list for Interbase and Firebird users :
firebird-support@yahoogroups.com
Nederlandse firebird nieuwsgroep :
news://newsgroups.firebirdsql.info