查询以计算预订的座位数量并留在屏幕中

时间:2015-04-06 09:54:24

标签: sql sql-server sql-server-2008 sql-server-2012

我正在尝试编写一个查询,以检查已填充的座位数量以及屏幕中连续多少个座位,但经过数小时的尝试后,我仍然无法理解它,我每次遇到一些错误时都尝试过很多查询改变where子句甚至是子查询:(

我使用的查询如下:

select 
    10 as [total seats in a row],
    perName as [movie name],
    seatRow as [seat row],
    (filledSeats - 10) as [seats left in a row]
    count(s.seatNo) filledSeats  
FROM 
     seats as s inner join 
     tickets as t on s.seatNo = t.seatNo inner join 
     performances as p on t.perID = p.perID 
 where 
     screenNo = '2' AND 
     perName = '50 shades of Grey'
 group by 
     perName

但即便是这个也给我错误

样本数据如下:

CREATE TABLE seats (
seatNo int IDENTITY (1,1) not null,
screenNo int,
screen varchar (20),
seatType Varchar (10),
seatRow varchar (5),
PRIMARY KEY (seatNo),
FOREIGN KEY (screenNo) REFERENCES screens
);
insert into seats values ('1','3', 'Normal', 'A');
insert into seats values ('1','3', 'Normal', 'B');
insert into seats values ('1','3', 'VIP', 'D');
insert into seats values ('1','1', 'Normal', 'F');
insert into seats values ('1','3', 'VIP', 'E');
insert into seats values ('1','2', 'VIP', 'C');
insert into seats values ('1','1', 'VIP', 'C');
insert into seats values ('2','2', 'Normal', 'D');
insert into seats values ('2','1', 'VIP', 'F');
insert into seats values ('2','2', 'Normal', 'B');
insert into seats values ('2','1', 'Normal', 'B');
insert into seats values ('2','2', 'VIP', 'B');
insert into seats values ('2','2',  'Normal', 'A');
insert into seats values ('3','2', 'VIP', 'B');
insert into seats values ('2','1', 'Normal', 'C');
insert into seats values ('3','3', 'Normal', 'E');
insert into seats values ('3','2', 'VIP', 'C');
insert into seats values ('3','3', 'VIP', 'A');
insert into seats values ('3','1', 'VIP', 'E');
insert into seats values ('3','1', 'Normal', 'D');   

CREATE TABLE performances( 
perID int IDENTITY (1,1)NOT NULL,  
perName varchar (50),
perTime Time,
perDate Date, 
perType varchar (20),
duration varchar (10),
screenNo int,
rating varchar (10),
location varchar (10),
PRIMARY KEY (perID),   
);
insert into performances values ('50 Shades of Grey', '12:00', '2015-03-08',     'Movie', '2hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '15:00', '2015-02-20', 'Movie', '2hrs', '2', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '18:00', '2015-02-26', 'Movie', '2hrs', '3', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '21:00', '2015-03-28', 'Movie', '2hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '12:00', '2015-03-26', 'Movie', '2:30hrs', '2', '18', 'Chelmsford');
insert into performances values ('Paddington', '15:00', '2015-03-03', 'Movie', '2:30hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '18:00', '2015-02-02', 'Movie', '2:3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Paddington', '21:00', '2015-03-31', 'Movie', '2:30hrs', '1', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '12:00', '2015-02-25', 'Movie', '3hrs', '2', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '15:00', '2015-02-06', 'Movie', '3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Shakespeare', '18:00', '2015-02-13', 'Movie', '3hrs', '3', '18', 'Chelmsford');
insert into performances values ('Shakespeare', '21:00', '2015-01-09', 'Movie', '3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '12:00', '2015-01-11', 'Movie', '2:3hrs', '1', '18', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '15:00', '2015-01-19', 'Movie', '2:30hrs', '2', '18', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '18:00', '2015-02-06', 'Movie', '2:3hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('Jupiter Ascending', '21:00', '2015-01-02', 'Movie', '2:3hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '12:00', '2015-01-06', 'Movie', '2hrs', '1', '18', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '15:00', '2015-03-28', 'Movie', '2hrs', '3', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '18:00', '2015-02-09', 'Movie', '2hrs', '2', 'PG', 'Chelmsford');
insert into performances values ('50 Shades of Grey', '21:00', '2015-02-06', 'Movie', '2hrs', '2', '18', 'Chelmsford');


CREATE TABLE bookings( 
     bookingID int IDENTITY (1,1) NOT NULL,  
     customerID int ,
     clerkID varchar (20) ,
     perID int,
     ticketBooked int, 
     bookingDate Date,   
     bookingTime time,
     bookingType varchar(20),
     totalCost decimal(5,2),
     PRIMARY KEY (bookingID),
     FOREIGN KEY (customerID) REFERENCES customers,
     FOREIGN Key (clerkID) REFERENCES clerks, 
    );  

insert into bookings values (16, 'clerk04', 1, 2, '2015-02-24', '12:00', 'Normal', '20');
insert into bookings values (2, 'clerk02', 20,3, '2015-02-25', '15:00', 'Advance', '34');
insert into bookings values (10, 'clerk04', 3,5, '2015-02-17', '18:00', 'Advance', '75');
insert into bookings values (5, 'clerk01', 19,2, '2015-01-24', '21:00', 'Advance', '30');
insert into bookings values (13, 'clerk01', 4,1, '2015-02-05', '12:00', 'Normal', '10');
insert into bookings values (20, 'clerk04', 5,1,'2015-01-26', '15:00', 'Normal', '12');
insert into bookings values (4, 'clerk03', 6, 3,'2015-02-09', '18:00', 'Advance', '30');
insert into bookings values (1, 'clerk03', 2, 1,'2015-01-03', '21:00', 'Advance', '15');
insert into bookings values (17, 'clerk04', 3, 1,'2015-02-09', '12:00', 'Normal', '12');
insert into bookings values (9, 'clerk02', 18,2,'2015-02-02', '15:00', 'Advance', '20');
insert into bookings values (11, 'clerk01', 6,3,'2015-02-10', '18:00', 'Advance', '45');
insert into bookings values (18, 'clerk01',1 ,1,'2015-02-06', '21:00', 'Normal', '12');
insert into bookings values (8, 'clerk05', 5,2,'2015-01-13', '12:00', 'Advance', '20');
insert into bookings values (6, 'clerk02', 7,2,'2015-02-05', '15:00', 'Advance', '20');
insert into bookings values (10, 'clerk03', 8,1,'2015-02-14', '18:00', 'Normal', '15');
insert into bookings values (14, 'clerk01', 9,2,'2015-01-19', '21:00', 'Normal', '20');
insert into bookings values (7, 'clerk05', 10, 3,'2015-01-29', '12:00', 'Advance','34');
insert into bookings values (3, 'clerk01', 12, 1,'2015-02-18', '15:00', 'Normal', '12');
insert into bookings values (13, 'clerk02', 15,3,'2015-01-08', '18:00', 'Normal', '45');
insert into bookings values (19, 'clerk03', 6, 2,'2015-01-27', '21:00', 'Normal', '30');

 CREATE Table tickets (
 ticketNo int  IDENTITY (1,1) Not Null,
 bookingID int,  
 perID int,
 seatNo int,
 ticketType varchar(10),
 ticketDate date, 
 ticketPrice decimal (4,2),
 screen int,
 perTime time,
 PRIMARY KEY (ticketNo),
 FOREIGN KEY (bookingID) REFERENCES bookings,
 FOREIGN KEY (perID) REFERENCES performances, 
 FOREIGN KEY (seatNo) REFERENCES seats,
 ); 
insert into tickets values (20, 1, 1,'Student', '2015-01-12', '10 ', '1', '15:00');
insert into tickets values (19, 7, 2,'VIP', '2015-01-11', '15', '2', '21:00');
insert into tickets values (18, 1, 3,'Adult', '2015-02-21', '12', '2', '15:00');
insert into tickets values (17, 19, 4,'Adult', '2015-03-25', '12', '1', '18:00');

3 个答案:

答案 0 :(得分:0)

您还没有显示ticketsbookings的任何架构(或数据),虽然您的查询中没有使用它的架构和数据,所以我只能猜测你是什么想。

那就是说,这是你的查询(我相信)纠正了语法问题:

select 
  10 as [total seats in a row],
  perName as [movie name],
  seatRow as [seat row],
  count(s.seatNo) - 10 as [seats left in a row],
  count(s.seatNo) filledSeats
FROM seats as s
join tickets as t on s.seatNo = t.seatNo
join performances as p on t.perID = p.perID 
where screenNo = '2'
AND perName = '50 shades of Grey'
group by perName, seatRow

关于您报告的错误,您会收到该错误,因为一旦您为表提供别名,该表就会被有效地重命名为该别名的查询 - 因此您无法引用完整的表名。 / p>

performances.perName更改为p.perName

答案 1 :(得分:0)

您可以尝试这样的事情

SELECT
 s.seatType,
 COUNT(*) as [total seats in a row],
 perName as [movie name],
 seatRow as [seat row],
 COUNT(*) - SUM(CASE WHEN t.perID IS NOT NULL THEN 1 ELSE 0 END) as [seats left in a row],
 SUM(CASE WHEN t.perID IS NOT NULL THEN 1 ELSE 0 END) filledSeats
 FROM performances as p
    INNER JOIN seats as s
ON s.screenNo = p.screenNo
LEFT JOIN 
    (
        SELECT b.perid ,t.seatNo
        FROM bookings b
        INNER JOIN tickets as t 
        ON  t.bookingid = b.bookingid
    )T
 ON s.seatNo = t.seatNo
    AND T.perid = p.perid
 WHERE p.perid = 1
 GROUP BY perName, s.seatType,seatRow

您可以查看此SQL Fiddle

由于相关ID不正确,我更改了一些输入数据。架构有冗余列。

  • 您的seats表格引用了screen表格以及除{FK screen以外的screenNo列。
  • Tickets有一个FK表演和预订(反过来有FK表现)。这些列看起来也是多余的(ticketDate, screen and pertime
  • Location表格中的
  • performance看起来是screen或其他位置特定表格的属性,应由screen引用。

答案 2 :(得分:0)

解决问题;一步一步:

  1. 错过(filledSeats - 10) as [seats left in a row] ,后。
  2. filledSeats不是您表格中的有效字段,那么它来自哪里?
    如果它是下一个字段,则应再次count(s.seatNo),因为您无法在当前查询中使用别名
  3. screenNo在您的查询中也不是有效字段,因为它需要一个前缀来确定它来自s.screenNop.screenNo
  4. seatRow之后的字段select没有任何聚合函数时,您应该在group by之后调用它。
  5. 所以下面的查询将没有错误;但需要一些编辑:

    select 
        10 as [total seats in a row],
        perName as [movie name],
        seatRow as [seat row],
        (count(s.seatNo) - 10) as [seats left in a row],
        count(s.seatNo) as filledSeats  
    FROM 
         seats as s inner join 
         tickets as t on s.seatNo = t.seatNo inner join 
         performances as p on t.perID = p.perID 
     where 
         p.screenNo = '2' AND 
         perName = '50 shades of Grey'
     group by 
         perName, seatRow
    

    因此,如果您使用它,您的结果是:

    total seats in a row | movie name           | seat row | seats left in a row | filledSeats
    ---------------------+----------------------+----------+---------------------+------------
    10                   | 50 Shades of Grey    | F        | -9                  | 1
    

    如果您将(count(s.seatNo) - 10)更改为(10 - count(s.seatNo)),则会9而不是-9