mysql - Use sequential CASEs as parameters for larger CASE -


select case when     case         when `aa`>6         `a`         else null     end,     case         when `bb`>6         `b`         else null     end,     case         when `cc`>6         `c`         else null     end `name` end table; 

the goal of query show columns through c , name. if a-c null not show name. i'm new cases , 'advanced' queries in general, if have set fool, feel free let me know.

thanks reading.

note: doesn't compile

the logic i'm trying so:

if(     if(aa>6) 1 else 0     or     if(bb>6) 1 else 0     or     if(cc>6) 1 else 0     etc,etc    ) 1 else 0 

table:

name          b        c         aa   bb  cc -------------------------------------------------------- name1   data1   null    data3       3   n   15 name2   null    data2   data4       n   2   8 name3   null    data2   data4       n   2   2 

output wanted if it's showing values gt 6:

 name        b      c  -------------------------  name1              data3  name2              data4 

one way it

select name,         case when aa > 6 end a,        case when bb > 6 b end b,        case when cc > 6 c end c   table1  aa > 6     or bb > 6     or cc > 6 

or

select name,         case when aa > 6 end a,        case when bb > 6 b end b,        case when cc > 6 c end c  table1 having coalesce(a, b, c, -1) <> -1 

in second query -1 constant returned if of columns have null values. can use value never part of result set. in particular case since you're applying column > 6 condition every column can use number less 6.

output:

 |  name |      |      b |     c | ----------------------------------- | name1 | (null) | (null) | data3 | | name2 | (null) | (null) | data4 | 

here sqlfiddle demo


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

visual studio - TFS will not accept changes I've made to a Java project -