Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Multiple columns in SQL Query into same row in Excel

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-03-20 : 09:06:36
Jon writes "I am joining three tables in a SQL Query and exporting the data into an Excel Spreadsheet. I'm not sure how to explain the problem (still new at most of this), so here is a kind of representation...

The Spreadsheet currently looks like this (same way SQL pulls it)

LastName FirstName Class Date
Doe John Class1 3/18/2007
Doe John Class2 3/27/2007
Doe John Class3 4/21/2007
(etc)

I need it to look like this:

LastName FirstName Class1 Class2 Class3
Doe John 3/18/2007 3/27/2007 4/21/2007


Sorry if that's confusing, but any help you could provide would be much appreciated. I am using SQL Server 2000 Standard (no service pack at the moment) and Windows 2K SP4 (on server). Desktop that Excel spreadsheet is going to is Windows XP SP2."

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-20 : 09:18:43
[code]
select LastName, FirstName,
Class1 = max(case when Class = 'Class1' then Date end),
Class2 = max(case when Class = 'Class2' then Date end),
Class3 = max(case when Class = 'Class3' then Date end)
from table
group by LastName, FirstName
[/code]


KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-04-02 : 05:54:20
Also, you can make use of EXCEL's PIVOT feature

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -