简体   繁体   English

在sql情况下when语句为NULL

[英]NULL in sql case when statements

I'm sorting people by their age in columns with case when statements, so 我将按年龄将人们按年龄分类,并带有case when语句,因此

select 
        case when age >= 11 and age <= 20 then name end column1 as "11-20",
        case when age >= 21 and age <= 30 then name end column2 as "21-30",
        case when age >= 31 and age <= 40 then name end column3 as "31-40"
from passenger

The result is something like that: 结果是这样的:

11-20    21-30    31-40
John     NULL     NULL
NULL     Kevin    NULL
Michael  NULL     NULL
NULL     NULL     Beckey

and I want output to look like: 我希望输出看起来像:

11-20    21-30    31-40
John     Kevin    Beckey
Michael 

So how do I remove NULL or move not NULL values up I'm using MySQL 所以我该如何删除NULL或将非NULL值向上移动?

Based on the Oracle(?) answer of Gordon Lindoff, you can do it in MySQL like this: 根据戈登·林多夫(G​​ordon Lindoff)的Oracle(?)答案,您可以在MySQL中执行以下操作:

select
  max(case when agegroup = '11-20' then name end) as "11-20",
  max(case when agegroup = '21-30' then name end) as "21-30",
  max(case when agegroup = '31-40' then name end) as "31-40"
from 
  (select @rownum1 := 0, @rownum2 := 0, @rownum3 := 0) v,
  (select
     case
       when Age > 10 and Age <= 20 then '11-20' 
       when Age > 20 and Age <= 30 then '21-30' 
       when Age > 30 and Age <= 40 then '31-40'
     end as agegroup,
     case
       when Age > 10 and Age <= 20 then @rownum1 := @rownum1 + 1 
       when Age > 20 and Age <= 30 then @rownum2 := @rownum2 + 1 
       when Age > 30 and Age <= 40 then @rownum3 := @rownum3 + 1
     end as seqnum,
     name
   from
     Passengers) p
group by
  seqnum

Demo: http://sqlfiddle.com/#!2/5ea1a/9 演示: http : //sqlfiddle.com/#!2/5ea1a/9

Fiddle with the given data. 摆弄给定的数据。

You remove the NULL s by doing a group by . 通过对group by进行group by可以删除NULL But, you don't have a key for the group by . 但是,您没有group by的密钥。 Most databases support the row_number() function. 大多数数据库都支持row_number()函数。 With that function you can do: 使用该功能,您可以执行以下操作:

select max(case when agegroup = '11-20' then name end) as "11-20",
       max(case when agegroup = '21-30' then name end) as "21-30",
       max(case when agegroup = '31-40' then name end) as "31-40"
from (select p.*, row_number() over (partition by agegroup order by name) as seqnum
      from (select p.*,
                   (case when age >= 11 and age <= 20 then '11-20'
                         when age >= 21 and age <= 30 then '21-30'
                         when age >= 31 and age <= 40 then '31-40'
                    end) as agegroup
            from passenger p
           ) p
group by seqnum;

EDIT: 编辑:

The equivalent in MySQL uses variables: MySQL中的等效项使用变量:

select max(case when agegroup = '11-20' then name end) as "11-20",
       max(case when agegroup = '21-30' then name end) as "21-30",
       max(case when agegroup = '31-40' then name end) as "31-40"
from (select p.*,
             @rn := if(agegroup = @agegroup, @rn + 1, 1) as seqnum,
             @agegroup := agegroup
      from (select p.*,
                   (case when age >= 11 and age <= 20 then '11-20'
                         when age >= 21 and age <= 30 then '21-30'
                         when age >= 31 and age <= 40 then '31-40'
                    end) as agegroup
            from passenger p
           ) p cross join
           (select @rn := 0, @agegroup := '') var
       order by agegroup
      ) p
group by seqnum;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM