Friday, 15 August 2014

sql - How to count same Id in a column in a table and based on count perform concatenation -


I have a table in the format

The name of the name is 1 Amit 2 Mohit 2 Nahar.

>

I confirm that this table will be updated in the format

the name of the name is 1 Amit 2 Mohit or Nahar.

Is there any question in SQL which solves this Can the purpose?

try it

  DECLARE @Table TABLE (id INT, name Include VARCHAR (Max), @Table (ID, [NAME]) SELECT 1, to enter 'Amit' (ID, [NAME]) SELECT 2, 'Mohit' INSERT INT @Table (ID, [NAME] ]] 2, select 'ID' to enter 'Nahar' (ID, [name]), enter 'C' (ID, [name]) SELECT 3, 'D' INSERT @ NAME]) Select SELECT 3, 'E'  

query (for first query)

  ID, REPLACE (accessories ( ('' By name '+' + name + '' @ in table B. where BID = AI Enable a group by merge data @ from the xml path ('')), 1,1, '', '', ',' OR ' 

output

query (change request)

  ID merge data 1 Amit 2 Mohit or Nahar  P> 
  Select one ID, case when inserted (x.cnt, 0) & lt; = 2 then substitute (accessories (select ',' + '+ name +' from @tables where b.id = path for xml path '')), 1,1, '', ',', 'OR') when x.cnt> 2 Then change (accessories ('select', 'to +' '+ name +' '' table 'B where b.id = xm path for a.id (' ')), 1,' ',', ' , 'And'), leaving an interesting data from @ an adid = x.id on the x (id)> id, select the id from the @Table group, / code>  

Output:

  id merge 1 amit 2 captcha or naver 3c and d & e  

No comments:

Post a Comment