Need a single SQL SELECT for one to many relationship Tables

  • Hi all,

    I have two tables with one to many relationship. Can i get the line table data separated by comma within a single select statement in sql 2005.

    Eg:

    Table 1:

    A B

    -------

    1 XX

    2 YY

    Table 2:

    C D

    ---------

    1 P

    1 Q

    1 R

    2 S

    2 T

    I need the below output with single sql select statement:

    A B Result

    ----------------------------

    1 XX P,Q,R

    2 YY S,T

    Is this possible.. Kindly clarify..

    Thanks & Regards,

    Sridevi

  • Sridevi (11/3/2008)


    Hi all,

    I have two tables with one to many relationship. Can i get the line table data separated by comma within a single select statement in sql 2005.

    Eg:

    Table 1:

    A B

    -------

    1 XX

    2 YY

    Table 2:

    C D

    ---------

    1 P

    1 Q

    1 R

    2 S

    2 T

    I need the below output with single sql select statement:

    A B Result

    ----------------------------

    1 XX P,Q,R

    2 YY S,T

    Is this possible.. Kindly clarify..

    Thanks & Regards,

    Sridevi

    yes this is possible, let me know the query so that i could modify it

    kshitij kumar
    kshitij@krayknot.com
    www.krayknot.com

  • CREATE TABLE Table1(A INT, B VARCHAR(10))

    GO

    CREATE TABLE TABLE2(C INT, D INT, E VARCHAR(10))

    GO

    Table1 DATA:

    A B

    ------

    1 XX

    2 YY

    Table2 DATA:

    C D E

    --------------

    1 1 P

    1 2 Q

    1 3 R

    2 1 S

    2 2 T

    My QUERY:

    SELECT TABLE1.A, TABLE1.B,TABLE2.C,TABLE2.D,TABLE2.E FROM

    TABLE1 A JOIN TABLE2 B ON A.A = B.C AND A.A = 1

    RESULT COMES AS:

    A B C D E

    -------------------

    1 XX 1 1 P

    1 XX 1 2 Q

    1 XX 1 3 R

    Instead of three rows, i need the result as below:

    A B C D E

    ----------------------------

    1 XX 1 1,2,3 P,Q,R

    Is this can be done without using cursor/loop?

    Regards,

    Sridevi

  • Sridevi (11/3/2008)


    CREATE TABLE Table1(A INT, B VARCHAR(10))

    GO

    CREATE TABLE TABLE2(C INT, D INT, E VARCHAR(10))

    GO

    Table1 DATA:

    A B

    ------

    1 XX

    2 YY

    Table2 DATA:

    C D E

    --------------

    1 1 P

    1 2 Q

    1 3 R

    2 1 S

    2 2 T

    My QUERY:

    SELECT TABLE1.A, TABLE1.B,TABLE2.C,TABLE2.D,TABLE2.E FROM

    TABLE1 A JOIN TABLE2 B ON A.A = B.C AND A.A = 1

    RESULT COMES AS:

    A B C D E

    -------------------

    1 XX 1 1 P

    1 XX 1 2 Q

    1 XX 1 3 R

    Instead of three rows, i need the result as below:

    A B C D E

    ----------------------------

    1 XX 1 1,2,3 P,Q,R

    Is this can be done without using cursor/loop?

    Regards,

    Sridevi

    Loop is required for such sort of output, why dont you create a function that performs this on the desired columns

    kshitij kumar
    kshitij@krayknot.com
    www.krayknot.com

  • Many Thanks to you.

    I wrote a function and i got the result too.

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SET @p = '' ;

    SELECT @p = @p + D + ','

    FROM TABLE2

    WHERE C = @a;

    RETURN @p

    END

    SELECT A,dbo.select_concat(A)

    FROM TABLE1

    GROUP BY A

    Thanks again.

    Sridevi

  • Sridevi (11/3/2008)


    Many Thanks to you.

    I wrote a function and i got the result too.

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SET @p = '' ;

    SELECT @p = @p + D + ','

    FROM TABLE2

    WHERE C = @a;

    RETURN @p

    END

    SELECT A,dbo.select_concat(A)

    FROM TABLE1

    GROUP BY A

    Thanks again.

    Sridevi

    Heh... is the trailing comma that leaves ok with you? If not, try this, instead...

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS

    BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SELECT @p = COALESCE(@p +',') + D

    FROM dbo.TABLE2

    WHERE C = @a;

    RETURN @p

    END

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ... and, before you get all fired up and start using that or any other concatenation function, you have to read the following to avoid some serious performance problems...

    http://www.sqlservercentral.com/articles/Test+Data/61572/

    It also has an alternate method that uses "STUFF" that works really well.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (11/3/2008)


    Sridevi (11/3/2008)


    Many Thanks to you.

    I wrote a function and i got the result too.

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SET @p = '' ;

    SELECT @p = @p + D + ','

    FROM TABLE2

    WHERE C = @a;

    RETURN @p

    END

    SELECT A,dbo.select_concat(A)

    FROM TABLE1

    GROUP BY A

    Thanks again.

    Sridevi

    Heh... is the trailing comma that leaves ok with you? If not, try this, instead...

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS

    BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SELECT @p = COALESCE(@p +',') + D

    FROM dbo.TABLE2

    WHERE C = @a;

    RETURN @p

    END

    Or this:

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS

    BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SELECT

    @p = COALESCE(@p +',', '') + D

    FROM

    dbo.TABLE2

    WHERE

    C = @a;

    RETURN @p

    END

  • How about this approach? It may be faster than using an inline function.

    [font="Courier New"]--create temp tables

    CREATE TABLE #Table1

    (

        A INT

       ,B VARCHAR(10)

    )

    CREATE TABLE #Table2

    (

        C INT

       ,D INT

       ,E VARCHAR(10)

    )

    --insert sample data into our tables

    INSERT INTO #Table1

    SELECT 1,    'XX'

    UNION ALL SELECT 2,    'YY'

    INSERT INTO #Table2

    SELECT 1,     1,        'P'

    UNION ALL SELECT 1,     2,        'Q'

    UNION ALL SELECT 1,     3,        'R'

    UNION ALL SELECT 2,     1,        'S'

    UNION ALL SELECT 2,     2,        'T'

    --create a CTE of the tables joined

    -- this will allow us to join the CTE to itself to create our data

    ;WITH All_Data AS

    (

       SELECT  A.A

               ,A.B

               ,B.C

               ,B.D

               ,B.E

       FROM #TABLE1 A

       JOIN #TABLE2 B ON A.A = B.C

    )

    -- return A, B, C as is

    -- group by those columns to get distinct values

    -- columns D, E should be CSV

    SELECT A

       ,B

       ,C

        ,STUFF((SELECT ',' + CAST(B.D AS VARCHAR)

               FROM All_Data B

                WHERE A.A = B.A

               FOR XML PATH('')),1,1,'') AS D

        ,STUFF((SELECT ',' + CAST(B.E AS VARCHAR)

               FROM All_Data B

                WHERE A.A = B.A

               FOR XML PATH('')),1,1,'') AS E

    FROM All_Data A

    WHERE A.A = 1 -- only return data for 1, if you remove this you will get all rows

    GROUP BY A

           ,B

           ,C

    -- clean up

    DROP TABLE #TABLE1

    DROP TABLE #TABLE2

    [/font]

  • Lynn Pettis (11/3/2008)


    Jeff Moden (11/3/2008)


    Sridevi (11/3/2008)


    Many Thanks to you.

    I wrote a function and i got the result too.

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SET @p = '' ;

    SELECT @p = @p + D + ','

    FROM TABLE2

    WHERE C = @a;

    RETURN @p

    END

    SELECT A,dbo.select_concat(A)

    FROM TABLE1

    GROUP BY A

    Thanks again.

    Sridevi

    Heh... is the trailing comma that leaves ok with you? If not, try this, instead...

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS

    BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SELECT @p = COALESCE(@p +',') + D

    FROM dbo.TABLE2

    WHERE C = @a;

    RETURN @p

    END

    Or this:

    CREATE FUNCTION select_concat (@A INT )

    RETURNS VARCHAR(MAX) AS

    BEGIN

    DECLARE @p VARCHAR(MAX) ;

    SELECT

    @p = COALESCE(@p +',', '') + D

    FROM

    dbo.TABLE2

    WHERE

    C = @a;

    RETURN @p

    END

    Dang it... thanks for the catch, Lynn...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply