If I have a query that selects the top 5 rows of a table, is there a way to manipulate the SQL to present the values from these five rows as five columns?
Existing Query:
SELECT TOP 5 FirstName
FROM USER
Existing Result
- Jon
- Bill
- Jill
- Lori
- Rick
Desired Result
- Jon, Bill, Jill, Lori, Rick
*Note: * I’ve presented the query here in SQL Server syntax, but I would prefer a generally applicable solution.
Note: Replace
ROW_NUMBER()(which works in SQL Server) with any function that will allow you to number the records in the inner query; such asrownum, etc, depending on your version of SQL.