English 中文(简体)
页: 1
原标题:SQL Sum of Sums

我在不同的表格中计算了计数总额。 这是每张<代码>的两次。 现在我想得到这两笔款项。

下面是我目前所花两笔款项的代码:

    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 

我怎么将这两笔款项汇在一起?

最佳回答

我永远不会与欧洲妇联竞争,但我不得不说一下这个问题。

如果增加白色空间,我希望你看到我所说的话:

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

打破了,只有<>>>>>,因为你回到两个牢房是分析职能,也是工会。

  1. You re doing a cartesian join between booking and production, this means that you multiply the number of rows in each by each other.
  2. Your sub-selects on performance are returning one value, which is already known. There s no reason to do them at all.
  3. You re implicitly converting numbers into strings and back into numbers again.
  4. You re scanning a table or index 8 times here!

看来,如果你想要每个业绩的合计数额,可以把你的询问简化到以下几个方面:

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
                )

Please note the explicit join syntax, this has been around for a few decades, makes things a lot clearer and helps stop mistakes. This query will do two range scans, one of booking and one of production, assuming you have indexes on performanceid on both tables. It ll also do a unique scan of performance assuming that performanceid is the primary key of this table.

作为对这项工作的解释,我现在最后设法使你的计划正确! 我们选择两种性能:12。 然后,我们选择与这些生产有关的每一种生产,并选择与这些生产相关的所有图书。 您可以进一步简化,这取决于表<代码>price。 然后,我们拿到每个<代码>price的账号,并将这些账面产品的总价值计算出来。

作为建议的一部分,我谨always建议了解您期望从<>>之前的查询中恢复哪些价值。 最好能够并且确实犯错。 由于你能够看到回归的数值不正确,能够赶上这些数值,将会有所帮助。

Further Reading:

问题回答

1. 使用亚,和总额

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)




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签