Need PIVOT or other query help for SQL Server

I have a table tbl_M_CDS_Curves with the following fields:
Market_Date (Date), Curve_Name (varchar), Fixed_Term (int), Spread (real).

I want to query the value of spread for each unique curve_name and fixed_term for two dates, Date1 and Date2 and get this kind of result:

Curve_Name1, Fixed_Term1, Spread on Date1, Spread on Date2
Curve_Name1, Fixed_Term2, Spread on Date1, Spread on Date2
Curve_Name1, Fixed_Term3, Spread on Date1, Spread on Date2
Curve_Name1, Fixed_TermN, Spread on Date1, Spread on Date2
Curve_Name2, Fixed_Term1, Spread on Date1, Spread on Date2
Curve_Name2, Fixed_Term2, Spread on Date1, Spread on Date2
Curve_Name2, Fixed_Term3, Spread on Date1, Spread on Date2
Curve_Name2, Fixed_TermN, Spread on Date1, Spread on Date2

There is only one row in the table for each date, curve_name and fixed_term.

The documentation on PIVOT is just too abstract for me. Any help will be most appreciated!

We are running SQL Server 2008.
jkurantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
The way you describe the problem, it does not sound like you would need a PIVOT. You could run two separate queries, and join the results together.

Something like:
SELECT A.Curve_Name, A.Fixed_Term, A.Spread [First_Date], B.Spread [Second_Date]
FROM (SELECT Curve_Name, Fixed_Term, Spread FROM tbl_M_CDS_Curves WHERE Market_Date = '<Date1>') A
LEFT JOIN ((SELECT Curve_Name, Fixed_Term, Spread FROM tbl_M_CDS_Curves WHERE Market_Date = '<Date2>') B ON A.Curve_Name = B.Curve_Name AND A.Fixed_Term = B.Fixed_Term
ORDER BY A.Curve_Name, A.Fixed_Term

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkurantAuthor Commented:
I am getting this error message:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'ON'.
jkurantAuthor Commented:
Got it! You just had an extra ( with ((. Also, my field is Curve, not Curve_Name, it turns out, so the correct query is

SELECT A.Curve, A.Fixed_Term, A.Spread [First_Date], B.Spread [Second_Date]
FROM (SELECT Curve, Fixed_Term, Spread FROM tbl_M_CDS_Curves WHERE Market_Date = '2014-12-31') AS A
LEFT JOIN (SELECT Curve, Fixed_Term, Spread FROM tbl_M_CDS_Curves WHERE Market_Date = '2015-01-02') AS B
ON A.Curve = B.Curve AND A.Fixed_Term = B.Fixed_Term
ORDER BY A.Curve, A.Fixed_Term
jkurantAuthor Commented:
Apparently, I didn't need a pivot at all. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.