大家好我想查询一下,了解与男性顾客相比,有多少女性顾客预订了50种色调。经过几个小时的尝试,我不得不寻求帮助。我将非常感谢您的帮助:(我使用以下代码:
select
customerGender,
ticketbooked,
bookingType,
ticketType,
perName,
t.ticketNo,
ticketPrice,
ticketbooked * ticketPrice as TotalPrice
from bookings as b inner join customers as c
on b.customerID = c.customerID
inner join tickets as t
on b.bookingID = t.bookingID
inner join performances as p
on t.perID =p.perID
where c.customerGender = 'F' and (Select count(*) as total from customers)
非常感谢您的帮助
样本数据:
CREATE TABLE customers (
customerID int NOT NULL IDENTITY (1,1),
customerName varchar(20),
customerEmail varchar(30),
customerGender varchar(10),
contactNo varchar(50),
PRIMARY KEY(customerID),
);
(1, 'Lawrence', 'lmartinez0@slashdot.org', 'M', '2-(697)619-4497'),
(2, 'Jean', 'jcollins1@howstuffworks.com', 'F', '7-(857)278-7098'),
(3, 'Paula', 'plong2@virginia.edu', 'F', '8-(341)167-3714'),
(4, 'Adam', 'agutierrez3@youku.com', 'M', '2-(632)402-7122'),
(5, 'Kelly', 'kjones4@ca.gov', 'F', '6-(794)653-4190'),
(6, 'Nancy', 'nsanders5@fastcompany.com', 'F', '3-(712)734-4770'),
(7, 'Judith', 'jwatson6@furl.net', 'F', '4-(327)227-4957'),
(8, 'George', 'gburke7@fc2.com', 'M', '4-(272)395-7114'),
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');
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');
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'
我想在M和F客户之间找到差异,看看预订中哪一个更高。
答案 0 :(得分:0)
尝试此查询:
SELECT
COUNT(customerGender) as numberOfFemaleOrders,
SUM(ticketbooked) as totalOrderedTickets
from bookings as b
inner join customers as c
on b.customerID = c.customerID
inner join performances p
on p.perID=b.perID
WHERE p.perName='50 Shades of Grey' AND c.customerGender='F'
如果您只对预订感兴趣,则无需加入tickets
表。
答案 1 :(得分:0)
我第一次回答问题就好了。
你不能将以下内容添加到SELECT
列表吗?
SELECT ticketbooked
,bookingType
,ticketType
,perName
,t.ticketNo
,ticketPrice
,ticketbooked * ticketPrice AS TotalPrice
,'Female Customer' = count(CASE
WHEN c.customerGender = 'F'
THEN 1
ELSE NULL
END)
,'Male Customer' = count(CASE
WHEN c.customerGender = 'M'
THEN 1
ELSE NULL
END)
FROM bookings AS b
INNER JOIN customers AS c ON b.customerID = c.customerID
INNER JOIN tickets AS t ON b.bookingID = t.bookingID
INNER JOIN performances AS p ON t.perID = p.perID
答案 2 :(得分:0)
您可以使用如下查询:
WITH TicketCount_CTE
AS (SELECT CASE
WHEN C.customerGender = 'F' THEN B.ticketBooked
ELSE 0
END AS TicketsBooked_Female,
CASE
WHEN C.customerGender = 'M' THEN B.ticketBooked
ELSE 0
END AS TicketsBooked_Male
FROM performances AS P
INNER JOIN bookings AS B
ON P.perID = B.perID
AND P.perName = '50 Shades of Grey'
INNER JOIN customers AS C
ON B.customerID = C.customerID)
SELECT SUM(TicketsBooked_Female) AS FemaleTicketsCount,
SUM(TicketsBooked_Male) AS MaleTicketsCount
FROM TicketCount_CTE