-6
TeamName        Patient Name         Staff Name
Team 1C DMAC                         Sabirah Bibi
Team 1C DMAC                         Shubhi Rastogi
Team 1C DMAC    Andy Proctor    
Team 1C DMAC    Lynn Ashurst    

This four need to change like this

TeamName        Patient Name             Staff Name
Team 1C DMAC    Andy Proctor             Sabirah Bibi
Team 1C DMAC    Lynn Ashurst             Shubhi Rastogi

Thanks in Advance for updating the Sql Query

5
  • Give The SQL is not a good title. Didn't you try anything? Jun 24, 2014 at 9:16
  • 2
    Why do you combine Andy Proctor with Sabirah Bibi and not with Shubhi Rastogi? What is the rule? Jun 24, 2014 at 9:17
  • Yeah. He can try putting some money up to have someone else do his job. COme on, user, that is disgusting - zero effort shown, just entitlement. We are not a programmers social security.
    – TomTom
    Jun 24, 2014 at 9:17
  • He Did Use CamelCase..
    – NickyvV
    Jun 24, 2014 at 9:33
  • Some advice... Firstly, don't just ask for someone to write you a query, it's much better if you attempt a solution yourself and show what you have tried and state what didn't work. Secondly, looking at the question, there is no clear answer as you haven't provided enough details about why the rows should be merged how they are. You've not supplied any logic about why row 1 merges with 3 and row 2 merges with 4. Are there any other fields involved? Are there always multiple rows with the same team name? Should the rows just be merged in the sequence they are added?
    – Tanner
    Jun 24, 2014 at 9:44

2 Answers 2

0

Try this

update t
    Set staffname = b.Staffname
    from 
    (
    Select teamname,patientname,row_number() over(order by teamname) 'id'
    from   Team
    where  staffname is null
    ) a
    join 
    (
    Select teamname,staffname,row_number() over(order by teamname) 'id'
    from team 
    where patientname is null
    ) b
    on a.teamname=b.teamname and a.id=b.id
    join team t
    on  t.teamname = a.teamname and t.patientname = a.patientname
    where t.staffname is null

    Delete from team where patientname is null
0
create table #temp
(
TeamName        varchar(50),
PatientName         varchar(100),
StaffName varchar(200)
)
insert into #temp
select 'Team 1C DMAC ','','Sabirah Bibi'

union

select 'Team 1C DMAC ','','Shubhi Rastogi'

union

select 'Team 1C DMAC ','Andy Proctor',''

union

select 'Team 1C DMAC ','Lynn Ashurst',''

union

select 'Team 1C DMAC ','Lynn Ashurst','Andy Proctor'



update #Temp set StaffName=T1.StaffName from #Temp

inner join 
(

select T1.Patientname,T2.StaffName,T1.TeamName from 
(

select TeamName,PatientName,StaffName,Row_number() over(partition by TeamName order by 
PatientName) as ID from #temp
where StaffName ='' or Patientname !=''

) as T1

inner join 

(

select TeamName,PatientName,StaffName,Row_number() over(partition by TeamName order by 
PatientName) as Id from #temp
where StaffName !='' or Patientname =''

) as T2

on (T1.Id= T2.Id and (T1.StaffName='' or T2.patientName='') and T1.TeamName=T2.Teamname)
or(T1.StaffName!='' and T2.patientName!='' and T1.TeamName=T2.Teamname)

) as T1

on #Temp.TeamName=T1.TeamName
and #Temp.PatientName=T1.PatientName
and #Temp.StaffName=''

delete from #temp where patientName=''

select * from #temp'
3
  • Are you having one single quote too many at the end of select * from #temp'?
    – Pang
    Oct 12, 2018 at 7:33
  • it is a mistake when i copy in the site,just remove that
    – Sivaraj S
    Oct 12, 2018 at 8:55
  • Then I suggest you to update your answer by clicking on the edit link under the post. Thank you.
    – Pang
    Oct 12, 2018 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.