我有一个看起来像这样的表: Table
然而,问题在于,不是说Patricia在同一行中有13张票和1张电影票,而是分成不同的行。
我想我需要做一个数据透视表,但我不确定我到底需要做什么。
到目前为止,这是我的代码:
select customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
count(ticketdetails.eventtype) as 'Theater Tickets',
0 as 'Movie Tickets'
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType ='T'
Group by Customer.hippcode
union
select customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
0, count(ticketdetails.eventtype) as 'Movie Tickets'
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType ='M'
Group by Customer.hippcode
order by `theater tickets` + `movie tickets` desc;
提前感谢您的帮助。
答案 0 :(得分:0)
您可以尝试这样的事情:
select
customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
count(case when ticketdetails.eventtype = 'T' then 1 else 0 end) as TheaterTickets,
count(case when ticketdetails.eventtype = 'M' then 1 else 0 end) as MovieTickets
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType in ('T', 'M')
Group by customer.hippcode, customer.LastName, customer.Firstname, customer.Email
Order by TheaterTickets+MovieTickets desc;