dbTalk Databases Forums  

Converting Rows to Columns in SQL7

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Converting Rows to Columns in SQL7 in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Brad Joss
 
Posts: n/a

Default Converting Rows to Columns in SQL7 - 08-06-2003 , 01:38 AM






I do a:
SELECT * FROM xxx

And Get:
Date Place Sum
A M 1
A O 3
A P 2
B O 5
B M 4
B P 2

And I want it to look like:

Date M O P
A 1 3 2
B 4 5 2

Can you think of an EASY way to do this?
I can do it with a cursor that constructs a SQL statement, which I EXEC, but the 8000 character limit may prove to be a limiting factor.
sp_execsql is somewhat messy for the nature of this issue.
Any input is appreciated.

Thanks in advance.

Reply With Quote
  #2  
Old   
Simon Hayes
 
Posts: n/a

Default Re: Converting Rows to Columns in SQL7 - 08-06-2003 , 04:37 AM






"Brad Joss" <bradjoss (AT) hotmail (DOT) com> wrote

Quote:
I do a:
SELECT * FROM xxx

And Get:
Date Place Sum
A M 1
A O 3
A P 2
B O 5
B M 4
B P 2

And I want it to look like:

Date M O P
A 1 3 2
B 4 5 2

Can you think of an EASY way to do this?
I can do it with a cursor that constructs a SQL statement, which I EXEC,
but the 8000 character limit may prove to be a limiting factor.
sp execsql is somewhat messy for the nature of this issue.
Any input is appreciated.

Thanks in advance.
--
Assuming that (Date, Place) is a unique combination, then this should work:

select
Date,
sum(case when Place = 'M' then Sum else NULL end) as 'M',
sum(case when Place = 'O' then Sum else NULL end) as 'O',
sum(case when Place = 'P' then Sum else NULL end) as 'P'
from
xxx
group by
Date

Simon


Reply With Quote
  #3  
Old   
Rajesh Garg
 
Posts: n/a

Default Re: Converting Rows to Columns in SQL7 - 08-06-2003 , 06:29 AM



i tried something but couldnt do anything about it. only if you send
me the solution you have it will help me(though with the limiting
factor)

Else i think at any point of time the 8000 character limit will be a
factor that will not allow you to do this always........
RVG


"Brad Joss" <bradjoss (AT) hotmail (DOT) com> wrote

Quote:
I do a:
SELECT * FROM xxx

And Get:
Date Place Sum
A M 1
A O 3
A P 2
B O 5
B M 4
B P 2

And I want it to look like:

Date M O P
A 1 3 2
B 4 5 2

Can you think of an EASY way to do this?
I can do it with a cursor that constructs a SQL statement, which I EXEC,
but the 8000 character limit may prove to be a limiting factor.
sp execsql is somewhat messy for the nature of this issue.
Any input is appreciated.

Thanks in advance.
--

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.