• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

how to get multiple rows result in a single row

I have table which has two columns one RecordTypeID and second is Price.
RecordTypeID column is uses to store multiple types of records in this single table
RecordType can – Pending = 1
            Closed = 2
            Listings = 3
Right now, if I run this sql
Select  Price from PropertyProfit Where RecordTypeID (1,2,3)
I get 3 rows.

Now, I need to write an sql query which can return in format and in a single row.
Pending  Closed Listings
1       2        3

Can any one help me out here      
1 Solution
u can do this using the pivot option in SQL server. Read here for more :
Scott PletcherSenior DBACommented:
Not sure I fully understand what you need, but here's my best guess as of now:

    MAX(CASE WHEN RecordType = 1 THEN Price END) AS [Pending],
    MAX(CASE WHEN RecordType = 2 THEN Price END) AS [Closed],
    MAX(CASE WHEN RecordType = 3 THEN Price END) AS [Listings]
FROM ropertyProfit
Where RecordTypeID IN (1,2,3)

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now