嘿,我正在使用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)
这给了我一个结果:
337.5
然后我有另一个问题,计算出总特许权资金:
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' )
这给了我以下结果:
18
现在我想要一种方法,我可以从第一个查询中减去第二个查询的结果。可能吗?我该怎么办?我认为它与子查询有关,但我不太确定。
有任何帮助吗? 感谢。
答案 0 :(得分:5)
你可以这样做:
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;
答案 1 :(得分:1)
您可以使用括号
在select语句中选择select (put your second query here) - (put your first select here) from dual
答案 2 :(得分:0)
尝试使用以下内容:
SELECT Total_Money_Made - Total_Consession_Money FROM
( SELECT SUM(bookings * CategoryPrice )AS Total_Money_Made, ( 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' )
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) )