Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help in Logic to Concat

Author  Topic 

madhuvrk
Starting Member

2 Posts

Posted - 2014-08-22 : 03:52:59
Hello everyone,

This is the requirement,I have a table like this below :

Actual table: Item_Seller

item_id |seller_name
-----------------------
12310450 |Pro Team
12310450 |CSNStores.com
12310451 |CSNStores.com
12310452 |CSNStores.com
12310452 |Pro Team
12310453 |Pro Team
12310691 |CSNStores.com
12310691 |Pro Team
12310692 |CSNStores.com
12310692 |Pro Team

The requirement is to concatenate seller_name in the table like below output, this should not be performed using a stored procedure. I need help in writing a select query to display the below output.

item_id |seller_name_concat
------------------------------
12310450 |Pro Team,CSNStores.com
12310451 |CSNStores.com
12310452 |CSNStores.com,Pro Team
12310453 |Pro Team
12310691 |CSNStores.com,Pro Team
12310692 |CSNStores.com,Pro Team


Thanks
Madhuvrk

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2014-08-22 : 05:58:55
can you try this
SELECT item_id,
MAX(CASE WHEN rk = 1 THEN seller_name END) AS value1,
MAX(CASE WHEN rk = 2 THEN seller_name END) AS value2
into #temp1
FROM (SELECT item_id, seller_name,
ROW_NUMBER() OVER(PARTITION BY item_id
order by item_id) AS rk
FROM item_seller) AS A
GROUP BY item_id
select item_id,value1+','+value2 as concat_seller from #temp1
drop table #temp1


Javeed Ahmed
Go to Top of Page
   

- Advertisement -