sql - PostgreSQL 选择语句

标签 sql postgresql

给出下表:

 shows:

      title      | basic_ticket_price
 ----------------+--------------------
  Inception      |              $3.50
  Romeo & Juliet |              $2.00

performance:

  perf_date  | perf_time |     title
 ------------+-----------+----------------
  2012-08-14 | 00:08:00  | Inception
  2012-08-12 | 00:12:00  | Romeo & Juliet

booking:
  ticket_no | perf_date  | perf_time | row_no | person_id
 -----------+------------+-----------+--------+-----------
          1 | 2012-08-14 | 00:08:00  | P01    | 1
          2 | 2012-08-12 | 00:12:00  | O05    | 4
          3 | 2012-08-12 | 00:12:00  | A01    | 2

还有一个附加表:seat,其中包含编号与预订中的 row_no 相同的座位列表,并带有区域名称。

使用此语句对预订的座位进行分组:

select count(row_no) AS row_no,
       area_name 
from seat 
where exists (select row_no 
              from   booking 
              where booking.row_no = seat.row_no) 
group by area_name;

产生:

    row_no |  area_name
   --------+--------------
         1 | rear stalls
         2 | front stalls

我现在如何使用计数的行和area_name编写单个SQL语句来生成一个列表,显示演出名称、演出日期和时间以及每个区域的预订座位数?

我已经尝试过这个:

 select s.title,
        perf_date,
        perf_time,
        count(row_no) AS row_no,
        area_name 
 from shows s, 
      performance,
      seat 
 where exists (select row_no 
               from booking 
               where booking.row_no =  seat.row_no) 
 group by area_name,s.title,performance.perf_date,performance.perf_time;

但它显示重复的行:

      title      | perf_date  | perf_time | row_no |  area_name
 ----------------+------------+-----------+--------+--------------
  Romeo & Juliet | 2012-08-12 | 00:12:00  |      1 | rear stalls
  Romeo & Juliet | 2012-08-14 | 00:08:00  |      2 | front stalls
  Inception      | 2012-08-12 | 00:12:00  |      1 | rear stalls
  Inception      | 2012-08-14 | 00:08:00  |      2 | front stalls
  Inception      | 2012-08-14 | 00:08:00  |      1 | rear stalls
  Inception      | 2012-08-12 | 00:12:00  |      2 | front stalls
  Romeo & Juliet | 2012-08-14 | 00:08:00  |      1 | rear stalls
  Romeo & Juliet | 2012-08-12 | 00:12:00  |      2 | front stalls
  (8 rows)

任何解决此问题的帮助将不胜感激。

最佳答案

您应该考虑将 perf_date dateperf_time time 列合并到单个 timestamp 中。栏目:

perf_timestamp timestamp

如果您需要时间戳中的日期时间,只需按如下所示进行转换:

SELECT perf_timestamp::time;
SELECT perf_timestamp::date;

我通常建议使用代理主键。 “节目”(电影标题)的名称不是自然键 - 它不是唯一的。或者,正如 @user_unknown 已经提到的:开始时间并不是性能的实际主键。您可以使用 serial为此列。整个设置可能如下所示:

 -- show:
CREATE TEMP TABLE show (
 show_id serial PRIMARY KEY
,title text
,basic_ticket_price money  -- or numeric
);
INSERT INTO show (title, basic_ticket_price) VALUES
 ('Inception', 3.50)
,('Romeo & Juliet', 2.00);

-- performance:
CREATE TEMP TABLE performance (
 performance_id serial PRIMARY KEY
,show_id int REFERENCES show(show_id) ON UPDATE CASCADE
,perf_start timestamp
);
INSERT INTO performance (show_id, perf_start) VALUES
 (1, '2012-08-14 00:08')
,(2, '2012-08-12 00:12');

-- seat:
CREATE TEMP TABLE seat (
 row_no text PRIMARY KEY
,area_name text
);
INSERT INTO seat (row_no, area_name) VALUES
 ('P01', 'rear stalls')
,('O05', 'front stalls')
,('A01', 'front stalls');

-- booking:
CREATE TEMP TABLE booking (
 ticket_id serial PRIMARY KEY
,performance_id int REFERENCES performance(performance_id) ON UPDATE CASCADE
,row_no text REFERENCES seat(row_no) ON UPDATE CASCADE
,person_id int -- REFERENCES ?
);
INSERT INTO booking (performance_id, row_no, person_id) VALUES
 (1, 'P01', 1)
,(2, 'O05', 4)
,(2, 'A01', 2);

那么您的查询可能如下所示:

SELECT p.perf_start
      ,sh.title
      ,s.area_name
      ,count(*) booked
FROM   booking     b
JOIN   seat        s USING (row_no)
JOIN   performance p USING (performance_id)
JOIN   show        sh USING (show_id)
GROUP  BY 1,2,3
ORDER  BY 1,2,3;

结果:

 perf_start          | title          | area_name    | booked
---------------------+----------------+--------------+-------
 2012-08-12 00:12:00 | Romeo & Juliet | front stalls | 2
 2012-08-14 00:08:00 | Inception      | rear stalls  | 1

关于sql - PostgreSQL 选择语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859821/

相关文章:

php - 切换数据库mysql中的所有二进制值

mysql - 限制 SELECT 结果中长文本字段的长度

database - Postgres 等待事务完成

PostgreSQL 在插入触发器之前阻塞 0000-00-00 00 :00:00

sql - PostgreSQL - 替换 HTML 实体

SQL 查询连接来自不同 fdb 数据库的表

sql - 比较两个不同表中的列

mysql - SQL:将每个 ID 的值合并到一个变量中

postgresql - Postgres 数据库转储不想在 Docker 容器中导入

postgresql - 是否可以在版本 9.1 和 9.3 之间进行复制