Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RE:SQL Min Max and between

Status
Not open for further replies.

allyne

MIS
Feb 9, 2001
410
US
Hello,

I have data that looks like this:

Id Rec_ID Date Result
25 1 20140101 26.5
25 2 20140201 27.00
25 3 20140225 28.75

what I need is:

ID Date_1 Result_1 Date_2 Result_2 Date_3 Result_3
25 20140101 26.5 20140201 27.00 20140225 28.75

Does anyone have any suggestions as to how to achieve this?

Thanks so much for your help.
 
Like this:

Code:
Declare @Temp Table(Id Int, Rec_Id Int, Date DateTime, Result Decimal(10,2))

Insert Into @Temp Values(25, 1, '20140101', 26.5)
Insert Into @Temp Values(25, 2, '20140201', 27.00)
Insert Into @Temp Values(25, 3, '20140225', 28.75)

Select  Id, 
        Min(Case When Rec_Id = 1 Then Date End) As Date1,
        Min(Case When Rec_Id = 1 Then Result End) As Result1,
        Min(Case When Rec_Id = 2 Then Date End) As Date2,
        Min(Case When Rec_Id = 2 Then Result End) As Result2,
        Min(Case When Rec_Id = 3 Then Date End) As Date3,
        Min(Case When Rec_Id = 3 Then Result End) As Result3
From    @Temp
Group BY Id

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top