Help with query

  • Hi.

    I have two related tables, ex.

    Table A

    Rec_ID Value

    1 first_value

    2 second_value

    Table B

    Rec_ID Related_Value

    1 str1

    1 str2

    2 str3

    2 str4

    2 str5

    Both tables are related by Rec_ID

    I need to construct a query that return all the records in Table A and the related values in Table B concatenated as a single field, ex.

    Rec_ID Value Concatened_Value

    1 first_value 'str1 str2'

    2 second_value 'str3 str4 str5'

    What is the best way to accomplish that?

    Thanks in advanced.

  • What is it with concatenation!

    Not too sure where your heading with this:

    Rec_ID Value Concatened_Value

    1 first_value 'str1 str2'

    2 second_value 'str3 str4 str5'

    What exactly are you trying to acheive?

    If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!


    If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!

  • Hi myoldhouse,

    for relating you can use 'Outer Join'.

    But, as Andrew said, what do you want to do? Can you post an example with 'real' data?

    Cheers,

    Frank

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Sorry, but I don't know how to format the text in order to make my question clear, so I going to use _ to align the text correctly.

    Table A

    RecID______________Value

    1__________________first_value

    2__________________second_value

    Table B

    RecID______________Related_Value

    1__________________str1

    1__________________str2

    2__________________str3

    2__________________str4

    2__________________str5

    Both tables are related by Rec_ID

    I need to return all the records in Table A and the related values in Table B, concatenated as a single field, as follow.

    Query result:

    Rec_ID_____________Value____________Concatened_Value

    1__________________first_value_______'str1 str2'

    2__________________second_value_____'str3 str4 str5'

    Where Rec_ID and Value are the fields of table A and Concatened_Value are the related records of table B formated as string.

    If I construct the query

    SELECT Rec_ID, Value, Related_Value FROM TableA INNER JOIN TableB ON TableA.Rec_ID = TableB.Rec_ID

    I will obtain the following result

    Rec_ID_____________Value____________Concatened_Value

    1__________________first_value_______str1

    1__________________first_value_______str2

    2__________________second_value______str3

    2__________________second_value______str4

    2__________________second_value______str5

    and I need the result as I explain first.

    I hope that my question is a little bit clear now.

Viewing 4 posts - 1 through 3 (of 3 total)

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