Question: I have a table with ID and Dept.  

Table is

```
id  dept    salary  date
1   30      2000    8/25/2015 12:06:54.870 PM
2   20      5500    7/12/2015 12:06:54.870 PM
3   30      6700    11/21/2013 12:06:54.870 PM
4   30      8900    4/16/2009 12:06:54.870 PM
5   30      9900    6/29/2014 12:06:54.870 PM
6   10      1120    7/3/2015 12:06:54.870 PM
7   20      8900    4/13/2013 12:06:54.870 PM
8   10      2400    7/23/2015 12:06:54.870 PM
9   30      2600    8/21/2015 12:06:54.870 PM
10  10      2999    8/3/2015 12:06:54.870 PM

```

Just need the output like this   

```
Dept  ID
30     1,3,4,5,9

```

Answer: 
A bit simpler solution for those who want this to work for particular query:

```
DECLARE @res_csv  VARCHAR(10000)
BEGIN
    SELECT SomeIntField INTO #tmp FROM YourTblName WHERE 1=1 -- a hardcoded query
    UPDATE  #tmp
    SET     @res_csv = @res_csv + case when @res_csv is not NULL then ',' end + CONVERT(VARCHAR, SomeIntField) 
    drop table #tmp
    print @res_csv
END

```
I had similar problem and came up with the following solution:

```
select dept,list(id,',' order by dept) from TableName group by dept

```

Assuming `TableName` is the name of your table.