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. How to concatenate all columnB where columnA are the same? USING SQL 2005[modified]

How to concatenate all columnB where columnA are the same? USING SQL 2005[modified]

Scheduled Pinned Locked Moved Database
databasetutorialquestion
4 Posts 2 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.
  • S Offline
    S Offline
    Support123
    wrote on last edited by
    #1

    Hi, i have data like this in temp table Owner - DocumentPath 1 - c:\reports\fileA 1 - c:\reports\fileB 1 - c:\reports\fileC 2 - c:\reports\temp\FileA 2 - c:\reports\fileA 2 - c:\reports\fileB AND SO ON.... i would like to know... Is it possible to concatenate all DocumentPath column's into single column for each Owner ? so i would have a new temp table that looks like this (if possible... this is still theoretical) Owner - DocumentPaths 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC 2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB AND SO ON.... The reason i want to do this is because i need all the file paths in one line in one field on a report later... I thought i could PIVOT the data and then concatenate them... OR Is it possible to select only the top 3 (ordered by Modified Date DESCENDING) for each Owner??? like: SELECT TOP 3 Owner, DocumentPath FROM #tempTableDocPath ORDER BY Owner ASC,Modified DESC BUT for each owner show top 3 (if they have three) AND then Is it possible ONCE AGAIN to CONCATENATE the DocumentPath column into one column (in a new table / tempTable)? like this: Owner - DocumentPath 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC 2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB AND SO ON.... Thank you in advance. I hope i put it all down clearly :confused:

    "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

    modified on Wednesday, December 05, 2007 9:19:40 AM

    C 1 Reply Last reply
    0
    • S Support123

      Hi, i have data like this in temp table Owner - DocumentPath 1 - c:\reports\fileA 1 - c:\reports\fileB 1 - c:\reports\fileC 2 - c:\reports\temp\FileA 2 - c:\reports\fileA 2 - c:\reports\fileB AND SO ON.... i would like to know... Is it possible to concatenate all DocumentPath column's into single column for each Owner ? so i would have a new temp table that looks like this (if possible... this is still theoretical) Owner - DocumentPaths 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC 2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB AND SO ON.... The reason i want to do this is because i need all the file paths in one line in one field on a report later... I thought i could PIVOT the data and then concatenate them... OR Is it possible to select only the top 3 (ordered by Modified Date DESCENDING) for each Owner??? like: SELECT TOP 3 Owner, DocumentPath FROM #tempTableDocPath ORDER BY Owner ASC,Modified DESC BUT for each owner show top 3 (if they have three) AND then Is it possible ONCE AGAIN to CONCATENATE the DocumentPath column into one column (in a new table / tempTable)? like this: Owner - DocumentPath 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC 2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB AND SO ON.... Thank you in advance. I hope i put it all down clearly :confused:

      "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

      modified on Wednesday, December 05, 2007 9:19:40 AM

      C Offline
      C Offline
      coolestCoder
      wrote on last edited by
      #2

      Hi, 1. You can always write a stored procedure, wherein you can use cursor to concatenate the output of some SQL query. This string can then be returned / stored in some temporary table, which will be used later.

      Support123 wrote:

      Is it possible to select only the top 3 (ordered by Modified Date DESCENDING) for each Owner??? like: SELECT TOP 3 Owner, DocumentPath FROM #tempTableDocPath ORDER BY Owner ASC,Modified DESC BUT for each owner show top 3 (if they have three) AND then Is it possible ONCE AGAIN to CONCATENATE the DocumentPath column into one column (in a new table / tempTable)? like this: Owner - DocumentPath 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC 2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB AND SO ON....

      I didnt got this. Can you please clarify in details, what exactly you want in this case ??


      "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


      coolestCoder

      S 1 Reply Last reply
      0
      • C coolestCoder

        Hi, 1. You can always write a stored procedure, wherein you can use cursor to concatenate the output of some SQL query. This string can then be returned / stored in some temporary table, which will be used later.

        Support123 wrote:

        Is it possible to select only the top 3 (ordered by Modified Date DESCENDING) for each Owner??? like: SELECT TOP 3 Owner, DocumentPath FROM #tempTableDocPath ORDER BY Owner ASC,Modified DESC BUT for each owner show top 3 (if they have three) AND then Is it possible ONCE AGAIN to CONCATENATE the DocumentPath column into one column (in a new table / tempTable)? like this: Owner - DocumentPath 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC 2 - c:\reports\temp\FileAc:\reports\fileAc:\reports\fileB AND SO ON....

        I didnt got this. Can you please clarify in details, what exactly you want in this case ??


        "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


        coolestCoder

        S Offline
        S Offline
        Support123
        wrote on last edited by
        #3

        i want to put all the rows where the owner is the same (example 1... owner 1 is repeated three times because he has 3 documents linked to him... so he would look like this in the db: Owner - DocumentPath 1 - c:\reports\fileA 1 - c:\reports\fileB 1 - c:\reports\fileC) what i want to do is take all the paths linked to this owner, and put them into one column... to look like this. Owner - DocumentPath 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC So you see, now i have all the values needed in one column. This makes it TONS easier to display on a report. So Column 1 (Owner) would have the Owner id and the Column 2 (DocumentPath) will have all the DocumentPaths found linked to the owner in column 1. I hope this is more clear???

        "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

        C 1 Reply Last reply
        0
        • S Support123

          i want to put all the rows where the owner is the same (example 1... owner 1 is repeated three times because he has 3 documents linked to him... so he would look like this in the db: Owner - DocumentPath 1 - c:\reports\fileA 1 - c:\reports\fileB 1 - c:\reports\fileC) what i want to do is take all the paths linked to this owner, and put them into one column... to look like this. Owner - DocumentPath 1 - c:\reports\fileAc:\reports\fileBc:\reports\fileC So you see, now i have all the values needed in one column. This makes it TONS easier to display on a report. So Column 1 (Owner) would have the Owner id and the Column 2 (DocumentPath) will have all the DocumentPaths found linked to the owner in column 1. I hope this is more clear???

          "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

          C Offline
          C Offline
          coolestCoder
          wrote on last edited by
          #4

          Support123 wrote:

          I hope this is more clear???

          Yes, it is clear now. Can you provide some table structure for a more precise answer ? It would help me allot.


          "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


          coolestCoder

          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