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. SOLVED - Pivot a result set

SOLVED - Pivot a result set

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
2 Posts 2 Posters 1 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I need to create a subquery that pulls detail records for the header records in the main query. But I'd like to display values from the detail records in the form of a comma separated list, like so, "value1, value2, value3". And this list should appear as an extra column in the result set. Is there any way to achieve this in SQL Server? SOLUTION: Use the STRING_AGG function! Cool.

    The difficult we do right away... ...the impossible takes slightly longer.

    R 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I need to create a subquery that pulls detail records for the header records in the main query. But I'd like to display values from the detail records in the form of a comma separated list, like so, "value1, value2, value3". And this list should appear as an extra column in the result set. Is there any way to achieve this in SQL Server? SOLUTION: Use the STRING_AGG function! Cool.

      The difficult we do right away... ...the impossible takes slightly longer.

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      STRING_AGG is the best approach for SQL Server 2017 or later. If you need to support earlier versions, you'll need an alternative: Concatenating Row Values in Transact-SQL - Simple Talk[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      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