dbTalk Databases Forums  

How to?

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


Discuss How to? in the comp.databases.ms-sqlserver forum.



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

Default How to? - 06-05-2007 , 06:02 AM






I have table with following structure:

Cust Amount Type
1 150.00 1
1 100.00 2

I would like to get query result:

Cust Type1 Type2
1 150.00 0.00
1 0.00 100.00

How to do this with query?


Reply With Quote
  #2  
Old   
M A Srinivas
 
Posts: n/a

Default Re: How to? - 06-05-2007 , 07:23 AM






On Jun 5, 4:02 pm, Mirnes <leme... (AT) yahoo (DOT) com> wrote:
Quote:
I have table with following structure:

Cust Amount Type
1 150.00 1
1 100.00 2

I would like to get query result:

Cust Type1 Type2
1 150.00 0.00
1 0.00 100.00

How to do this with query?
create table #customer( cust int,amount numeric(10,2), type int)
insert into #customer values (1,150.00,1)
insert into #customer values (1,100.00,2)

select cust,
case when type = 1 then amount else 0.00 end as type1 ,
case when type = 2 then amount else 0.00 end as type2
from #customer

drop table #customer



Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: How to? - 06-05-2007 , 07:28 AM



You can use CASE, like this:

SELECT Cust,
CASE WHEN Type = 1
THEN Amount ELSE 0.0 END AS Type1,
CASE WHEN Type = 2
THEN Amount ELSE 0.0 END AS Type2
FROM Foobar

HTH,

Plamen Ratchev
http://www.SQLStudio.com





Reply With Quote
  #4  
Old   
Ed Murphy
 
Posts: n/a

Default Re: How to? - 06-05-2007 , 11:50 AM



Mirnes wrote:

Quote:
I have table with following structure:

Cust Amount Type
1 150.00 1
1 100.00 2

I would like to get query result:

Cust Type1 Type2
1 150.00 0.00
1 0.00 100.00

How to do this with query?
In addition to the CASE solution already posted, SQL Server 2005
allows some sort of explicit pivot functionality. I've never used
it, though, so I don't remember the syntax.


Reply With Quote
  #5  
Old   
--CELKO--
 
Posts: n/a

Default Re: How to? - 06-05-2007 , 01:00 PM



Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.




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.