Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. retrieve more than one row in one row

retrieve more than one row in one row

Scheduled Pinned Locked Moved Database
databasetutorialquestionlearning
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    obarahmeh
    wrote on last edited by
    #1

    Hi, I have an sql query that retrieves data for some ID where this ID has more than one data related to it. Example: ID Symbol 01 A 01 B 01 C of course the primary key is (ID, Symbol). when you select the ID and Symbol, it retieves the previous three rows. I need to retrieve these data in one row by concatinating the Symbol to itself; i.e: I need the result to be just like the following: ID Symbol 01 A and B and C Is this possible???

    Kind Regards OBarahmeh

    D L P 3 Replies Last reply
    0
    • O obarahmeh

      Hi, I have an sql query that retrieves data for some ID where this ID has more than one data related to it. Example: ID Symbol 01 A 01 B 01 C of course the primary key is (ID, Symbol). when you select the ID and Symbol, it retieves the previous three rows. I need to retrieve these data in one row by concatinating the Symbol to itself; i.e: I need the result to be just like the following: ID Symbol 01 A and B and C Is this possible???

      Kind Regards OBarahmeh

      D Offline
      D Offline
      Declan Bright
      wrote on last edited by
      #2

      You could do this using a cursor[^] to loop through the rows and build up the string. I wouldn't advise it for large resultsets as you could end up with performance issues.

      Declan Bright www.declanbright.com

      1 Reply Last reply
      0
      • O obarahmeh

        Hi, I have an sql query that retrieves data for some ID where this ID has more than one data related to it. Example: ID Symbol 01 A 01 B 01 C of course the primary key is (ID, Symbol). when you select the ID and Symbol, it retieves the previous three rows. I need to retrieve these data in one row by concatinating the Symbol to itself; i.e: I need the result to be just like the following: ID Symbol 01 A and B and C Is this possible???

        Kind Regards OBarahmeh

        L Offline
        L Offline
        leoinfo
        wrote on last edited by
        #3

        Yes, it is possible :)

        CREATE TABLE #T (ID varchar(2), Symbol varchar(1));
        INSERT INTO #T(ID, Symbol) SELECT '01', 'A'
        INSERT INTO #T(ID, Symbol) SELECT '01', 'B'
        INSERT INTO #T(ID, Symbol) SELECT '01', 'C'
        INSERT INTO #T(ID, Symbol) SELECT '02', 'B'
        INSERT INTO #T(ID, Symbol) SELECT '02', 'D'

        SELECT
        ID,
        STUFF(
        (SELECT ' and ' + Symbol FROM #T WHERE ID = t.ID FOR XML PATH(''))
        , 1
        , 5
        , ''
        ) AS Symbols
        FROM #T AS t
        GROUP BY ID

        DROP TABLE #T

        Please... SAVE my time by rating the posts that you read!


        There are 10 kinds of people in the world: those who understand binary and those who don't.

        P 1 Reply Last reply
        0
        • O obarahmeh

          Hi, I have an sql query that retrieves data for some ID where this ID has more than one data related to it. Example: ID Symbol 01 A 01 B 01 C of course the primary key is (ID, Symbol). when you select the ID and Symbol, it retieves the previous three rows. I need to retrieve these data in one row by concatinating the Symbol to itself; i.e: I need the result to be just like the following: ID Symbol 01 A and B and C Is this possible???

          Kind Regards OBarahmeh

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          SELECT SymbolA.Id
          ,Symbol=SymbolA.Symbol + ' and ' + SymbolB.Symbol + ' and ' + SymbolC.Symbol
          FROM Symbol SymbolA
          INNER JOIN Symbol SymbolB
          ON SymbolA.ID=SymbolB.ID
          AND SymbolB.Symbol='B'
          INNER JOIN Symbol SymbolC
          ON SymbolA.ID=SymbolC.ID
          AND SymbolC.Symbol='C'
          WHERE SymbolA.Symbol='A'

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            SELECT SymbolA.Id
            ,Symbol=SymbolA.Symbol + ' and ' + SymbolB.Symbol + ' and ' + SymbolC.Symbol
            FROM Symbol SymbolA
            INNER JOIN Symbol SymbolB
            ON SymbolA.ID=SymbolB.ID
            AND SymbolB.Symbol='B'
            INNER JOIN Symbol SymbolC
            ON SymbolA.ID=SymbolC.ID
            AND SymbolC.Symbol='C'
            WHERE SymbolA.Symbol='A'

            L Offline
            L Offline
            leoinfo
            wrote on last edited by
            #5

            Well... Let's just hope he is not using the whole alphabet for the Symbol field :)

            Please... SAVE my time by rating the posts that you read!


            There are 10 kinds of people in the world: those who understand binary and those who don't.

            P 1 Reply Last reply
            0
            • L leoinfo

              Well... Let's just hope he is not using the whole alphabet for the Symbol field :)

              Please... SAVE my time by rating the posts that you read!


              There are 10 kinds of people in the world: those who understand binary and those who don't.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              That's not in the scope of the problem as presented. :-D

              1 Reply Last reply
              0
              • L leoinfo

                Yes, it is possible :)

                CREATE TABLE #T (ID varchar(2), Symbol varchar(1));
                INSERT INTO #T(ID, Symbol) SELECT '01', 'A'
                INSERT INTO #T(ID, Symbol) SELECT '01', 'B'
                INSERT INTO #T(ID, Symbol) SELECT '01', 'C'
                INSERT INTO #T(ID, Symbol) SELECT '02', 'B'
                INSERT INTO #T(ID, Symbol) SELECT '02', 'D'

                SELECT
                ID,
                STUFF(
                (SELECT ' and ' + Symbol FROM #T WHERE ID = t.ID FOR XML PATH(''))
                , 1
                , 5
                , ''
                ) AS Symbols
                FROM #T AS t
                GROUP BY ID

                DROP TABLE #T

                Please... SAVE my time by rating the posts that you read!


                There are 10 kinds of people in the world: those who understand binary and those who don't.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I'm always amazed at the lack of functions in SQL Server. There should be a JOIN CONCAT aggregate function. There should also be TRIM (not just LTRIM and RTRIM), and PADding functions. Ah, well, an opportunity to write my first CLR aggregate function! :-D Ha! The example for creating a CLR aggregate is Concatenate! :laugh: Whoo hoo! Copied the example and tweaked it a little.

                SELECT ID
                ,Symbol=STUFF(dbo.Concatenate(' and '+Symbol),1,5,'')
                FROM Symbol
                GROUP BY ID
                ORDER BY ID
                ,Symbol

                modified on Thursday, July 31, 2008 11:33 AM

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  I'm always amazed at the lack of functions in SQL Server. There should be a JOIN CONCAT aggregate function. There should also be TRIM (not just LTRIM and RTRIM), and PADding functions. Ah, well, an opportunity to write my first CLR aggregate function! :-D Ha! The example for creating a CLR aggregate is Concatenate! :laugh: Whoo hoo! Copied the example and tweaked it a little.

                  SELECT ID
                  ,Symbol=STUFF(dbo.Concatenate(' and '+Symbol),1,5,'')
                  FROM Symbol
                  GROUP BY ID
                  ORDER BY ID
                  ,Symbol

                  modified on Thursday, July 31, 2008 11:33 AM

                  L Offline
                  L Offline
                  leoinfo
                  wrote on last edited by
                  #8

                  I don't know why you people are so scared of XML :)

                  Please... SAVE my time by rating the posts that you read!


                  There are 10 kinds of people in the world: those who understand binary and those who don't.

                  P 1 Reply Last reply
                  0
                  • L leoinfo

                    I don't know why you people are so scared of XML :)

                    Please... SAVE my time by rating the posts that you read!


                    There are 10 kinds of people in the world: those who understand binary and those who don't.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Who is? :confused:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups