sql - Multiple Case Statements to one row -


in sql server database 1 customer have many products.

when casing using this:

case     when br.ptype# = 'le' 'y'     else 'n' end [legal], case     when br.ptype# = 'br' 'y'     else 'n' end [br], case     when br.ptype# = 'ws' 'y'     else 'n' end [screen], case     when br.ptype# = 'tw' 'y'     else 'n' end [van] 

it returns:

title   firstname   lastname  email                 legal  br  screen  van mr      aaaa        aaaa      test.email@test.com   n      y   n       n mr      aaaa        aaaa      test.email@test.com   y      n   n       n 

how can returned results single row legal y , br y customer has these 2 policy types? know customer has 2 insurance types specified 4 i'm searching i'd return results like:

title  firstname  lastname  email               legal  br  screen  van mr     aaaa       aaaa      test.email@test.com y      y   n       n 

thanks in advance help.

select distinct --top 1000 dbo.parse_name_udf(yy.name#,'h') [title], dbo.parse_name_udf(yy.name#,'f') [firstname],   dbo.parse_name_udf(yy.name#,'l') [lastname], yy.email# [email], --br.polref@ [policyreference], --bc.datecreated# [date sold], --br.idat# [policystartdate], case when br.ptype# = 'le' 'y' else 'n' end [legal], case when br.ptype# = 'br' 'y' else 'n' end [br], case when br.ptype# = 'ws' 'y' else 'n' end [screen], case when br.ptype# = 'tw' 'y' else 'n' end [van]  brpolicy br left join yyclient yy on br.ref@ = yy.ref@ , br.b@ = yy.b@ left join brcledger bc on br.polref@ = bc.polref@ , br.b@ = bc.b@  br.ref@ = 'aaaa06' , (br.ptype# = 'ws' or br.ptype# = 'le' or br.ptype# = 'br' or br.ptype# = 'tw' ) 

please ignore br.ref@ clause that's test customer record

you can use max:

select  title,         firstname,         lastname,         max(case                 when br.ptype# = 'le' 'y'                 else 'n'             end) [legal],         max(case                 when br.ptype# = 'br' 'y'                 else 'n'             end) [br],         case             when br.ptype# = 'ws' 'y'             else 'n'         end [screen],         max(case                 when br.ptype# = 'tw' 'y'                 else 'n'             end) [van] dbo.yourtable group title,          firstname,          lastname 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -