0

我有一个passenger_information 表,其中的列是这样的booking_id, gender_id, passenger_name and passenger_age。我需要选择预订的男性和女性成人座位数量以及男性和女性儿童座位的数量。

gender_id表将 24 定义为男性,将 25 定义为女性。为了区分儿童座椅和成人座椅,我们使用passenger_age的儿童年龄不应超过 11 岁。

我正在尝试编写一个查询以获取基于booking_id但失败的详细信息。

有人可以帮我解决这个问题吗?

4

2 回答 2

0

尝试使用 Group by AND UNION。

    Select
    booking_id, 
    CASE gender_id WHEN 24 then 'Adult - Male'
    ELSE 'Adult - Female' END as 'Gender',
    count(booking_id)
FROM passenger_information
    WHERE passenger_age>11
    Group by booking_id,gender_id
UNION 
Select
    booking_id,  
    CASE gender_id WHEN 24 then 'Child - Male'
    ELSE 'Child - Female' END as 'Gender',
    count(booking_id)
FROM passenger_information
    WHERE passenger_age<=11
    Group by booking_id,gender_id
于 2012-07-30T12:03:58.557 回答
0

像这样的东西:

select 
      booking_id
      , sum(case when gender_id = 24 and passenger_age > 11 then 1 else 0 end) adult_male
      , sum(case when gender_id = 25 and passenger_age > 11 then 1 else 0 end) adult_female
      , sum(case when gender_id = 24 and passenger_age <= 11 then 1 else 0 end) child_male
      , sum(case when gender_id = 24 and passenger_age <= 11 then 1 else 0 end) child_female
from passenger_information
group by booking_id
于 2012-07-30T11:49:55.720 回答