3

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
5
  • Hello Nigel welcome to StackOverflow, next time try to provide a SqlFiddle so we can understand the problem better and give you an answer much faster – Also please read How to ask and How to create a Minimal, Complete, and Verifiable example. Commented Aug 26, 2015 at 17:59
  • Is this concatenation or is it more of a pivot. Commented Aug 26, 2015 at 18:31
  • should work just like this: stackoverflow.com/a/1785923/215752 Commented Aug 26, 2015 at 18:34
  • @hogan Q: Stuff and XML Path are generic tsql? because this questions say sybase. Commented Aug 26, 2015 at 18:37
  • @JuanCarlosOropeza - xml support and replace support exist in all modern db platforms. I don't know the details of sybase, so I didn't give an answer instead I made a comment. Commented Aug 26, 2015 at 18:38

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

This is the best way I know. Please do post if anyone knows a better solution:

I have named your table sal

DECLARE @id     INT
        , @max  INT
        , @dep  INT
        , @all  VARCHAR(255)

SELECT  @id = 1
        , @max = MAX(id)
FROM    sal

SELECT * INTO #tmp FROM sal

WHILE (1=1)
BEGIN

    SELECT  @dep = dept
    FROM    #tmp
    WHERE   id = @id

    IF @dep IS NULL
    BEGIN
        SELECT  @id = @id + 1

        IF @id > @max
            BREAK
        ELSE
            CONTINUE
    END

    UPDATE  #tmp
    SET     @all = @all + ',' + CONVERT(VARCHAR, id) 
    WHERE   dept = @dep

    --remove last comma
    select  @all = RIGHT(@all, LEN(@all)-1)

    DELETE  #tmp
    WHERE   dept = @dep

    -- selecting the output. insert into table if you want
    SELECT  @dep, @all

    SELECT  @dep   = NULL
            , @all = NULL

    SELECT  @id = @id + 1

    IF @id > @max
        BREAK

    -- fail safe
    IF @id > 100
        BREAK
END

drop table #tmp

Comments

-3

Depending on the size of your table, this query might not be ideal. But it worked for me when I tried it, assuming TableName is the name of your table:

SELECT dept, LIST(id, ',' ORDER BY dept) AS ID_List 
FROM TableName 
GROUP BY dept;

Here's a detailed explanation:

  • SELECT dept: This selects the department column from the table.
  • LIST(id, ',' ORDER BY dept) AS ID_List: This function concatenates the IDs into a comma-separated list within each department group. The ORDER BY dept ensures that the IDs are sorted within each department.
  • FROM TableName: Specifies the table from which data is being retrieved.
  • GROUP BY dept: Groups the results by department, so each row represents a unique department along with its corresponding concatenated list of IDs.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.