I have this data into the testDB on SQL Server:
I want write query to get this data from the table:
1 --------> 3
2 --------> 2
How can I do this?
Thanks.
and how can i get this data from up table?
1----->A,B,C
2----->D,E
I have this data into the testDB on SQL Server:
I want write query to get this data from the table:
1 --------> 3
2 --------> 2
How can I do this?
Thanks.
and how can i get this data from up table?
1----->A,B,C
2----->D,E
A simple GROUP BY
would do the trick.
SELECT
columnX,
COUNT(*)
FROM <YourTable>
GROUP BY columnX
For the other question:
SELECT
columnX,
STUFF((
SELECT ',' + columnY
FROM YourTable
WHERE columnX = t.columnX
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)')
,1 ,1 , '')
FROM YourTable t
GROUP BY t.columnX
or else use Window function in sql server:
Case 1:
select count(column x) over(partition by column x) as cnt,
column x,
column y from table
You can use this FOR XML PATH & STUFF trick for CASE 2:
SELECT
b.columnX,
COUNT(*) as cnt,STUFF( (SELECT ',' + a.column y
FROM table a
where a.column x = b.coulmn x
FOR XML PATH('')),
1, 1, '')
FROM table b
GROUP BY b.columnX