简体   繁体   中英

NULL in sql case when statements

I'm sorting people by their age in columns with case when statements, so

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

Based on the Oracle(?) answer of Gordon Lindoff, you can do it in MySQL like this:

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

Fiddle with the given data.

You remove the NULL s by doing a group by . But, you don't have a key for the group by . Most databases support the row_number() function. 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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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