【问题标题】:SQL select column multiple times with different conditionsSQL选择列多次不同的条件
【发布时间】:2015-12-08 19:04:31
【问题描述】:

我有一个事件表,其中包含事件中受伤人数的列和事件中死亡人数的列。 Incident 表与 Participants 表链接,您可以在其中找到一个人的年龄列,还有另一个表 ParticipantTypes 的列类型(例如,man(id = 1),woman(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

【问题讨论】:

    标签: mysql inner-join aggregate-functions


    【解决方案1】:
    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 
    

    【讨论】:

    • @Rohit Gaikwad 我猜比我快 :)
    【解决方案2】:

    试试这个....

    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
    

    【讨论】:

      【解决方案3】:
      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
      

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

      【讨论】:

        【解决方案4】:

        您在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
        

        【讨论】:

        • ipt.id = 1 in inner join 是打印错误,是的,我知道 where 子句中的 on。按年龄分组是必须的,让我们重写它: select ip.age as age, sum(i.number_injured) asjured where ipt.id = 1, sum(i.number_killed) as dead where ipt.id = 1, sum (i.number_injured) 受伤,其中 ipt.id = 2,sum(i.number_killed) 死亡,其中 ipt.id = 2,来自事件 i 内部加入参与者 ip on i.id = ip.incident_id 内部加入 ParticipantTypes ipt on ip .type_id = ipt.id 按 ip.age 分组
        • @AntoniyMilenkinski :根据问题的更新更新答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-23
        • 1970-01-01
        • 2021-11-20
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多