tags:

views:

174

answers:

2

I have a table in below format

ID Name 1 Amit 2 Mohit 2 Nahar

My reqiurement is to get a query that will update this table to below format

ID Name 1 Amit 2 Mohit OR NAHAR

Is there any query in SQL that can solve this purpose?

+1  A: 

You can try something like this to get the names concatenated.

DECLARE @Table TABLE(
     ID INT,
     NAME VARCHAR(MAX)
)

DECLARE @TempTable TABLE(    
     ID INT,
     NAME VARCHAR(MAX)
)

INSERT INTO @Table (ID,[NAME]) SELECT 1, 'A'
INSERT INTO @Table (ID,[NAME]) SELECT 2, 'B'
INSERT INTO @Table (ID,[NAME]) SELECT 2, 'C'

DECLARE @ID INT
DECLARE Cur CURSOR FOR 
SELECT  DISTINCT 
     ID
FROM    @Table

OPEN Cur
FETCH NEXT FROM Cur INTO @ID

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @Name VARCHAR(MAX)
    SET @Name = NULL
    SELECT @Name = COALESCE(@Name + ' OR ' + NAME, Name)
    FROM @Table
    WHERE ID = @ID
    INSERT INTO @TempTable (ID,[NAME]) SELECT @ID, @Name
    FETCH NEXT FROM Cur INTO @ID
END

CLOSE Cur
DEALLOCATE Cur

SELECT * FROM @TempTable
astander
ThanksI am trying this!! and some other approach also using CTE.CAn you tell me is CTE faster than cursors.
AJ01
I think you will need to run some tests to check this. I have found that recursive CTE functions can become slow once you reach VERY deap.
astander
This link is helpfulhttp://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/Thanks a lot!!!
AJ01
A: 

Try this

DECLARE @Table TABLE(       
        ID INT,
        NAME VARCHAR(MAX)
)
    INSERT INTO @Table (ID,[NAME]) SELECT 1, 'Amit'
INSERT INTO @Table (ID,[NAME]) SELECT 2, 'Mohit'
INSERT INTO @Table (ID,[NAME]) SELECT 2, 'Nahar'
INSERT INTO @Table (ID,[NAME]) SELECT 3, 'C'
INSERT INTO @Table (ID,[NAME]) SELECT 3, 'D'
INSERT INTO @Table (ID,[NAME]) SELECT 3, 'E'

Query(For the first query)

select id,
     REPLACE(stuff((select ',' + '  ' + name  + '  ' 
      from @Table b
      where b.id = a.id
      FOR xml path('')),1,1,' '),',','OR') MergedData
from @Table a
group by a.id

Output:

id  MergedData
1      Amit  
2      Mohit  OR  Nahar

Query (For the change request)

select distinct a.id,

    case when coalesce(x.cnt,0) <= 2 then

     REPLACE(stuff((select ',' + '  ' + name  + '  ' 
      from @Table b
      where b.id = a.id
      FOR xml path('')),1,1,' '),',','OR') 


      when x.cnt > 2 then 

      REPLACE(stuff((select ',' + '  ' + name  + '  ' 
      from @Table b
      where b.id = a.id
      FOR xml path('')),1,1,' '),',','AND') 

      end

      MergedData
from @Table a

left join 

(select id,COUNT(id) cnt
from @Table 
group by ID
having (COUNT(id)>1))x
on a.ID = x.ID

Output:

id  MergedData
1      Amit  
2      Mohit  OR  Nahar  
3      C  AND  D  AND  E
priyanka.sarkar
What is this XML path??
AJ01
Can you please explain this query ?I think it will help me.Thanks in advance... :)
AJ01
I am using For Xml path to combine the items like Mohit, Nahar etc. The query is simple enough. I am picking up and combining the names based on the similar id's "b.id = a.id" and then using the stuff function I am removing the the first "," because the raw list will be ",Mohit,Nahar" and then by using the replace function I am replacing the "," with OR.Have a look here for understanding for xml pathhttp://geekswithblogs.net/chavansachinr/archive/2007/04/30/112132.aspx
priyanka.sarkar
Hey dude!! its working for me :)Thanks
AJ01
One more query i have condition that if count (Id) = 2 then concatenate OR to names but if count(Id)>2 then concatenate And.If you can help on this that will be great...
AJ01