SQL select 列多次使用不同的条件

SQL select column multiple times with different conditions

我有一个 table 事件,其中包含事件中受伤人数的列和事件中死亡人数的列。事件 table 与参与者 table 相关联,您可以在其中找到一个人的年龄列,还有另一个 table ParticipantTypes,其中包含类型列(男(id = 1),女( id = 2) 例如)。 所以,我想检索受伤的列号,类型为男性的列号被杀死,然后是受伤的列号,类型为女性的列号被杀死,全部按年龄分组。

select    
ip.age as age, 
sum(i.number_injured) as injured,
sum(i.number_killed) as killed
from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
where ipt.id = 1
group by ip.age

以下查询有效并仅检索 ParticipantTypes.type = 1 (man)

的结果

我想要它

select    
ip.age as age, 
sum(i.number_injured) as injured where ipt.id = 1,
sum(i.number_killed) as killed where ipt.id = 1, 
sum(i.number_injured) as injured where ipt.id = 2,
sum(i.number_killed) as killed where ipt.id = 2, 
from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

您正在 inner joinwhere.

中过滤掉具有 ipt.id = 1 条件的 woman 条记录

此外,您正在按 age 分组,这不是您想要的,正如我从问题中了解到的那样。所以,你的最终查询应该是。

编辑:根据相关更新的详细信息,以下是查询

select
    ip.age,
    sum(case when ipt.id =1 then i.number_injured else 0 end) as men_injured,
    sum(case when ipt.id =1 then i.number_killed else 0 end) as men_killed,
    sum(case when ipt.id =2 then i.number_injured else 0 end) as women_injured,
    sum(case when ipt.id =2 then i.number_killed else 0 end) as women_killed
from Incidents i
       inner join Participants ip on i.id = ip.incident_id
       inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age
select    
ip.age as age, 
sum(if(ipt.id=1,i.number_injured,0)) as injured_by_man,
sum(if(ipt.id=1,i.number_killed,0) as killed_by_man,
sum(if(ipt.id=2,i.number_injured,0)) as injured_by_woman,
sum(if(ipt.id=2,i.number_killed,0)) as killed_by_woman

from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

注意:上述代码未经测试。它可能包含小语法错误。

试试这个....

select    
ip.age as age, 
sum(case when ipt.id=1then i.number_injured else 0                
end) as injured_by_man,
sum(case when ipt.id=1then i.number_killed else 0  
end) as killed_by_man,
sum(case when ipt.id=2 then i.number_injured else 0 
end) as injured_by_woman,
sum(case when ipt.id=2 then i.number_killed else 0 
end) as killed_by_woman

from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age
select    
    ip.age as age, 
    sum(case when ipt.id=1 then i.number_injured    else 0 end)     as injured_man,
    sum(case when ipt.id=1 then i.number_killed     else 0 end)     as killed_man,
    sum(case when ipt.id=2 then i.number_injured    else 0 end)     as injured_woman,
    sum(case when ipt.id=2 then i.number_killed     else 0 end)     as killed_woman
 from 
    Incidents i
inner join 
    Participants ip on i.id = ip.incident_id
inner join 
    ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

示例结果:

age         injured_man killed_man  injured_woman killed_woman 
----------- ----------- ----------- ------------- ------------ 
         30          10           1             3            1 
         31          12           1             1            6 
         32          14           2             4            4