Sunday, 15 September 2013

select - How to write SQL 'case' Command? -



select - How to write SQL 'case' Command? -

i have grid view, , has type 1 , type 2 columns bind type1 , type2.

then add together cheek box it.

i want bind data, query info duplicated.

this query

select distinct userid, max(case when approvertypeid = 1 approvertypeid end) type1, max(case when approvertypeid = 2 approvertypeid end) type2 table1 grouping approvertypeid, userid

my original table looks this..

name type --------- name1 1 name2 1 name1 0

but want output this...

name type1 type2 ---------------- name1 1 0 name2 1 null

how can it?

you need remove approvertypeid of grouping clause because approvertypeid used in max() function. after can remove excessive distinct clause. , recieve want

select userid, max(case when approvertypeid = 1 approvertypeid end) type1, max(case when approvertypeid = 2 approvertypeid end) type2 table1 grouping userid

sql select

No comments:

Post a Comment