How to combine different rows in SQL Server? -
query:
select ag.agentid agentid, ag.agentname agentname, case when pc.contacttypecd='m' pc.contactnum end 'mobile', case when pc.contacttypecd='r' pc.contactnum end 'residence', case when pc.contacttypecd='em' pc.contactnum end 'emergency_no' agent ag left join party p on p.partyid = ag.partyid left join partycontact pc on pc.partyseq = p.partyseq ag.agentid = '10000005'
output
expected output
try this:
select ag.agentid agentid, ag.agentname agentname, max(case when pc.contacttypecd='m' pc.contactnum end) 'mobile', max(case when pc.contacttypecd='r' pc.contactnum end) 'residence', max(case when pc.contacttypecd='em' pc.contactnum end) 'emergency_no' agent ag left join party p on p.partyid = ag.partyid left join partycontact pc on pc.partyseq = p.partyseq ag.agentid = '10000005' group ag.agentid, ag.agentname
Comments
Post a Comment