希望有人可以帮助解决我在oracle中构建查询时遇到的问题。
我是oracle的新手,尽管我对sql有一些了解 -
这是一个电影预订系统场景 - 我试图让下面的sql输出一行,这是特定电影院特定演出所售票的总和。我已经到了下面,虽然它列出了所有12次出现,当我想要它说出售12张门票等。
我在查询构建器中初始化了这个,然后通过manualy添加了count和group。
对于我如何实现这一点,或者我可能出错的地方,我们都非常感激。
感谢您的光临。
select count(ticket.ticket_id) as tickets_sold, "PERFORMANCE"."PERFORMANCE_DATE" as "PERFORMANCE_DATE",
"FILM"."FILM_TITLE" as "FILM_TITLE",
"TICKET"."TICKET_ID" as "TICKET_ID",
"CINEMA"."LOCATION" as "LOCATION",
"PERFORMANCE"."PERFORMANCE_TIME" as "PERFORMANCE_TIME"
from "TICKET" "TICKET",
"RESERVATION" "RESERVATION",
"PERFORMANCE" "PERFORMANCE",
"RUN" "RUN",
"SCREEN" "SCREEN",
"CINEMA" "CINEMA",
"FILM" "FILM"
where "PERFORMANCE"."PERFORMANCE_ID"="RESERVATION"."PERFORMANCE_ID"
and "PERFORMANCE"."RUN_ID"="RUN"."RUN_ID"
and "RUN"."FILM_ID"="FILM"."FILM_ID"
and "RUN"."SCREEN_ID"="SCREEN"."SCREEN_ID"
and "SCREEN"."CINEMA_ID"="CINEMA"."CINEMA_ID"
and "TICKET"."RESERVATION_ID"="RESERVATION"."RESERVATION_ID"
and "PERFORMANCE"."PERFORMANCE_DATE" ='1-mar-09'
and "FILM"."FILM_TITLE" ='PIRATES OF THE CARIBBEAN - AT WORLDS END'
and "CINEMA"."LOCATION" ='bradfordeast'
and "PERFORMANCE"."PERFORMANCE_TIME" ='20:00' group by reservation.performance_id, performance.performance_date, film.film_title, ticket.ticket_id,cinema.location, performance.performance_time order by tickets_sold DESC;
输出:
TICKETS_SOLD PERFORMANCE_DATE FILM_TITLE TICKET_ID LOCATION PERFORMANCE_TIME
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 485 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 488 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 484 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 491 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 493 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 495 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 489 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 487 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 490 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 492 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 494 bradfordeast 20:00
1 01-MAR-09 PIRATES OF THE CARIBBEAN - AT WORLDS END 486 bradfordeast 20:00
答案 0 :(得分:1)
ticket_id
时包含COUNT(ticket_id)
和GROUP BY ticket_id
是没有意义的。
我推断您的架构具有如下关系:
TICKETS --> RESERVATION --> PERFORMANCE --> RUN --> SCREEN --> CINEMA
FILM --> RUN
看起来您想要的报告可以描述如下:
计算每部电影的票数,演出日期和时间。时间和电影院的位置。
在使用GROUP BY
的任何查询中,SELECT
- 列中的列与GROUP BY
列表中的列匹配很重要(除COUNT()
等聚合函数中的列外})。这称为单值规则,它在标准SQL中强制执行。这些是报告说明中“per”后面的列。
以下是我编写查询的方法:
SELECT COUNT(t."TICKET_ID") AS tickets_sold,
f."FILM_TITLE",
c."LOCATION",
p."PERFORMANCE_DATE",
p."PERFORMANCE_TIME",
FROM "TICKET" t,
JOIN "RESERVATION" r ON (r."RESERVATION_ID" = t."RESERVATION_ID")
JOIN "PERFORMANCE" p ON (p."PERFORMANCE_ID" = r."PERFORMANCE_ID")
JOIN "RUN" run ON (run."RUN_ID" = p."RUN_ID")
JOIN "SCREEN" s ON (s."SCREEN_ID" = run."SCREEN_ID")
JOIN "CINEMA" c ON (c."CINEMA_ID" = s."CINEMA_ID")
JOIN "FILM" f ON (f."FILM_ID" = run."FILM_ID")
WHERE p."PERFORMANCE_DATE" ='1-mar-09'
AND f."FILM_TITLE" ='PIRATES OF THE CARIBBEAN - AT WORLDS END'
AND c."LOCATION" ='bradfordeast'
AND p."PERFORMANCE_TIME" ='20:00'
GROUP BY f."FILM_TITLE", c."LOCATION", p."PERFORMANCE_DATE", p."PERFORMANCE_TIME"
ORDER BY tickets_sold DESC;
请注意,我使用标准JOIN
语法,将连接条件移动到ON
子句中。 WHERE
子句仅包含查询限制。从逻辑上讲,查询结果没有区别,但为了清楚起见,我更喜欢将连接条件保持在FROM
子句中的各个表附近。
我还使用单字母表别名来提高可读性(r
与run
除外,因为它们都以r开头。
答案 1 :(得分:0)
您需要从group子句中排除“ticket.ticket_id”(这个唯一值会产生不必要的组 - 结果字符串):
select count(ticket.ticket_id) as tickets_sold, "PERFORMANCE"."PERFORMANCE_DATE" as "PERFORMANCE_DATE",
"FILM"."FILM_TITLE" as "FILM_TITLE",
"TICKET"."TICKET_ID" as "TICKET_ID",
"CINEMA"."LOCATION" as "LOCATION",
"PERFORMANCE"."PERFORMANCE_TIME" as "PERFORMANCE_TIME"
from "TICKET" "TICKET",
"RESERVATION" "RESERVATION",
"PERFORMANCE" "PERFORMANCE",
"RUN" "RUN",
"SCREEN" "SCREEN",
"CINEMA" "CINEMA",
"FILM" "FILM"
where "PERFORMANCE"."PERFORMANCE_ID"="RESERVATION"."PERFORMANCE_ID"
and "PERFORMANCE"."RUN_ID"="RUN"."RUN_ID"
and "RUN"."FILM_ID"="FILM"."FILM_ID"
and "RUN"."SCREEN_ID"="SCREEN"."SCREEN_ID"
and "SCREEN"."CINEMA_ID"="CINEMA"."CINEMA_ID"
and "TICKET"."RESERVATION_ID"="RESERVATION"."RESERVATION_ID"
and "PERFORMANCE"."PERFORMANCE_DATE" ='1-mar-09'
and "FILM"."FILM_TITLE" ='PIRATES OF THE CARIBBEAN - AT WORLDS END'
and "CINEMA"."LOCATION" ='bradfordeast'
and "PERFORMANCE"."PERFORMANCE_TIME" ='20:00'
group by reservation.performance_id, performance.performance_date,
film.film_title, ticket.ticket_id,cinema.location, performance.performance_time
order by tickets_sold DESC;