查询返回相同的总和 - oracle sql

时间:2009-05-04 12:59:03

标签: sql oracle

想知道是否有人可以解释我正在使用的SQL查询;

我已经构建了这个查询;

SELECT  SUM(TICKET_TYPE.PRICE) AS TOTALCINEMASALES, CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
FROM    RESERVATION, TICKET, TICKET_TYPE, CINEMA, PERFORMANCE
WHERE   TICKET_TYPE.TICKET_TYPE_ID = TICKET.TICKET_TYPE_ID
        AND TICKET.RESERVATION_ID = RESERVATION.RESERVATION_ID
        AND RESERVATION.PERFORMANCE_ID = PERFORMANCE.PERFORMANCE_ID
        AND CINEMA.LOCATION = 'SKIPTON'
        AND PERFORMANCE.PERFORMANCE_DATE BETWEEN to_date('01/03/2009','DD/MM/yyyy') AND to_date('07/04/2009','DD/MM/yyyy')
GROUP BY
       CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
ORDER BY
       TOTALCINEMASALES;

每次我运行它时,它会返回每行的sam总和数量,但是我知道数据库中存在不同级别的票证销售等,它会为我改变的每个订单执行,任何指针都是我怎么能改进它?

由于

2 个答案:

答案 0 :(得分:2)

你忘了加入CINEMA,可能还有PERFORMANCE。

猜猜:

AND CINEMA.CINEMA_ID = PERFORMANCE.CINEMA_ID

您可以使用ANSI连接,当您习惯它时,它实际上更容易阅读:

SELECT  SUM(TICKET_TYPE.PRICE) AS TOTALCINEMASALES, CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
FROM    
   TICKET 
   inner join TICKET_TYPE
     on TICKET_TYPE.TICKET_TYPE_ID = TICKET.TICKET_TYPE_ID
   inner join RESERVATION
     on TICKET.RESERVATION_ID = RESERVATION.RESERVATION_ID
   inner join PERFORMANCE
     on RESERVATION.PERFORMANCE_ID = PERFORMANCE.PERFORMANCE_ID
   inner join CINEMA  /* the missing link */
     on CINEMA.CINEMA_ID = PERFORMANCE.CINEMA_ID
WHERE
   CINEMA.LOCATION = 'SKIPTON'
   AND PERFORMANCE.PERFORMANCE_DATE BETWEEN to_date('01/03/2009','DD/MM/yyyy') AND to_date('07/04/2009','DD/MM/yyyy')
GROUP BY
       CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
ORDER BY
       TOTALCINEMASALES;

答案 1 :(得分:0)

SELECT  SUM(TICKET_TYPE.PRICE) AS TOTALCINEMASALES, CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
FROM    RESERVATION, TICKET, TICKET_TYPE, CINEMA, PERFORMANCE
WHERE   TICKET_TYPE.TICKET_TYPE_ID = TICKET.TICKET_TYPE_ID
        AND TICKET.RESERVATION_ID = RESERVATION.RESERVATION_ID
        AND RESERVATION.PERFORMANCE_ID = PERFORMANCE.PERFORMANCE_ID
        AND CINEMA.LOCATION = 'SKIPTON'
-- Added this
        AND PERFORMANCE.CINEMA_ID = CINEMA.CINEMA_ID
--
        AND PERFORMANCE.PERFORMANCE_DATE BETWEEN to_date('01/03/2009','DD/MM/yyyy') AND to_date('07/04/2009','DD/MM/yyyy')
GROUP BY
        CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
ORDER BY
        TOTALCINEMASALES;