Dav*_*usa 4 sql oracle multiple-select-query
嘿,我正在使用oracle sql来创建数据库.
我现在正在处理查询,我想出了两个单独的quires,我想合并以获得结果.
我创建的第一个查询是查找演出所产生的总金额:
SELECT SUM(bookings * CategoryPrice )AS Total_Money_Made
FROM ( SELECT CategoryPrice , count(*) AS bookings
FROM Booking b
JOIN performance per
ON b.performanceid = per.performanceid
JOIN Production p
ON per.productionid = p.productionid
WHERE per.performanceid IN (1, 2, 3, 4)
GROUP BY CategoryPrice)
Run Code Online (Sandbox Code Playgroud)
这给了我一个结果:
337.5
Run Code Online (Sandbox Code Playgroud)
然后我有另一个查询,计算出总的特许金:
SELECT SUM(ConsessionAmount * 2) AS Total_Consession_Money
FROM( SELECT COUNT (*) AS ConsessionAmount
FROM Booking
WHERE
Consession = 'Yes' AND PerformanceID = '1' OR
Consession = 'Yes' AND PerformanceID = '2' OR
Consession = 'Yes' AND PerformanceID = '3' OR
Consession = 'Yes' AND PerformanceID = '4' )
Run Code Online (Sandbox Code Playgroud)
这给了我以下结果:
18
Run Code Online (Sandbox Code Playgroud)
现在我想要一种方法,我可以从第一个查询中减去第二个查询的结果.可能吗?我该怎么办?我认为它与子查询有关,但我不太确定.
有帮助吗?谢谢.
你可以这样做:
WITH TOTAL_MADE
AS
(
SELECT SUM(bookings * CategoryPrice )AS Total_Money_Made
FROM ( SELECT CategoryPrice , count(*) AS bookings
FROM Booking b
JOIN performance per
ON b.performanceid = per.performanceid
JOIN Production p
ON per.productionid = p.productionid
WHERE per.performanceid IN (1, 2, 3, 4)
GROUP BY CategoryPrice)
), TOTAL_CONSESSION
AS
(
SELECT SUM(ConsessionAmount * 2) AS Total_Consession_Money
FROM( SELECT COUNT (*) AS ConsessionAmount
FROM Booking
WHERE
Consession = 'Yes' AND PerformanceID = '1' OR
Consession = 'Yes' AND PerformanceID = '2' OR
Consession = 'Yes' AND PerformanceID = '3' OR
Consession = 'Yes' AND PerformanceID = '4' )
)
SELECT
TOTAL_CONSESSION.Total_Consession_Money-
TOTAL_MADE.Total_Money_Made AS Something
FROM
TOTAL_MADE,
TOTAL_CONSESSION;
Run Code Online (Sandbox Code Playgroud)