我计算了不同表格中的计数总和。这是两次,每次performanceID
一次。现在我想得到两笔钱的总和。
以下是我目前所做的两笔钱的代码:
SELECT SUM((COUNT (BookingID) * CategoryPrice)) AS TotalAmount
FROM Booking, Production
WHERE Booking.PerformanceID IN(SELECT PerformanceID FROM Performance WHERE PerformanceID = '1')
and Production.ProductionID IN
(SELECT ProductionID FROM Performance WHERE PerformanceID = '1')
GROUP BY BookingID, CategoryPrice
UNION ALL
SELECT SUM((COUNT (BookingID) * CategoryPrice)) AS TotalAmount
FROM Booking, Production
WHERE Booking.PerformanceID IN(SELECT PerformanceID FROM Performance WHERE PerformanceID = '2')
and Production.ProductionID IN
(SELECT ProductionID FROM Performance WHERE PerformanceID = '2')
GROUP BY BookingID, CategoryPrice
我得到的结果是:
TOTALAMOUNT ----------- 70 60
我如何总结这两笔钱?
答案 0 :(得分:7)
我永远不会与FGITW竞争,但我不得不对这个问题说些什么......
如果我们添加空格,我希望你能看到我的意思:
SELECT SUM( (COUNT(BookingID) * CategoryPrice) ) AS TotalAmount
FROM Booking
, Production
WHERE Booking.PerformanceID IN ( SELECT PerformanceID
FROM Performance
WHERE PerformanceID = '1')
AND Production.ProductionID IN ( SELECT ProductionID FROM Performance
WHERE PerformanceID = '1')
GROUP BY BookingID, CategoryPrice
UNION ALL
SELECT SUM( (COUNT(BookingID) * CategoryPrice)) AS TotalAmount
FROM Booking
, Production
WHERE Booking.PerformanceID IN ( SELECT PerformanceID
FROM Performance
WHERE PerformanceID = '2')
AND Production.ProductionID IN ( SELECT ProductionID
FROM Performance
WHERE PerformanceID = '2')
GROUP BY BookingID, CategoryPrice
断开查询,只返回 的原因,即返回两行的是解析函数和union all。
booking
和production
之间进行了cartesian join,这意味着您可以将每个行中的行数相乘。performance
上的子选择返回一个已知的值。根本没有理由这样做。看起来好像您想要为每次演出获取总金额,在这种情况下,您的查询可以简化为以下内容:
SELECT SUM(bookings * CategoryPrice)
FROM ( SELECT CategoryPrice , count(*) as bookings
FROM Booking b
JOIN performance per
ON p.performanceid = per.performanceid
JOIN Production p
ON p.productionid = per.productionid
WHERE p.performanceid in (1, 2)
GROUP BY CategoryPrice
)
请注意显式连接语法,这已经存在了几十年,使事情变得更加清晰helps stop mistakes。假设两个表上的booking
都有索引,此查询将执行两个范围扫描,其中一个是production
,另一个是performanceid
。它还会对performance
执行唯一扫描,假设performanceid
是此表的主键。
作为对此的解释,现在我终于设法让你的架构正确了!我们选择了两个表演1
和2
。然后,我们选择与这些表演相关的每个作品以及与这些作品相关的每个预订。您可以根据categoryprice
所在的表格进一步简化此操作。然后,我们会根据categoryprice
获取预订数量,并将这些预算的总和相加以得出总价值。
作为一点建议,我总是建议您在接受查询正确之前了解您希望从查询返回的值。最好的可以而且确实会犯错误。因为你可以看到返回的值不正确而能够捕获它们会有所帮助。
答案 1 :(得分:0)
使用sub sql,并总计TotalAmount
SELECT SUM(TotalAmount)
( SELECT SUM((COUNT (BookingID) * CategoryPrice)) AS TotalAmount
FROM Booking, Production
WHERE Booking.PerformanceID IN(SELECT PerformanceID FROM Performance WHERE PerformanceID = '1')
and Production.ProductionID IN
(SELECT ProductionID FROM Performance WHERE PerformanceID = '1')
GROUP BY BookingID, CategoryPrice
UNION ALL
SELECT SUM((COUNT (BookingID) * CategoryPrice)) AS TotalAmount
FROM Booking, Production
WHERE Booking.PerformanceID IN(SELECT PerformanceID FROM Performance WHERE PerformanceID = '2')
and Production.ProductionID IN
(SELECT ProductionID FROM Performance WHERE PerformanceID = '2')
Group By CategoryPrice)