Hi i have a table like some "Demo" in that i have two Columns (ID,Name),
I have table:
I want to get table like:
How Can i write sql query for this ?
--TIA
Hi i have a table like some "Demo" in that i have two Columns (ID,Name),
I have table:
I want to get table like:
How Can i write sql query for this ?
--TIA
You have to use group by
clause with group_concat()
like:
select Id, group_concat(name) as name
from Demo
group by Id;
here group by Id
will make groups of data on id
bases and group_concat()
function concatenate the name.
SELECT DISTINCT id AS uqId, (SELECT GROUP_CONCAT(name_col) FROM table WHERE id = uqId) AS NAME FROM table;
try this query. This might help you.
Begin TRAN
CREATE TABLE #temp(Id INt,Event Nvarchar(25))
INSERT INTO #temp
SELECT 1,'IND'UNION ALL
SELECT 1,'USA'UNION ALL
SELECT 1,'AUS'UNION ALL
SELECT 1,'RUS'UNION ALL
SELECT 2,'DUBAI'UNION ALL
SELECT 2,'UK'UNION ALL
SELECT 3,'JAPAN'
SELECT DISTINCT ID, STUFF(
( SELECT ',' + [EVENT]
FROM #temp t1
WHERE ID = y.ID
FOR XML PATH (''))
, 1, 1, '')
FROM #temp y
ROLLBACK TRAN