Advertisement

05.15.2008 at 08:37AM PDT, ID: 23405588
[x]
Attachment Details

How can I use SQL SUM(DECODE... in Microsoft SQL Server 2005

Asked by JasonAsh in SQL Server 2005

Tags: Microsoft, SQL Server, 2005

Here's the code I run in Oracle SQL * Plus but I can't get the DECODE part to work in Microsoft SQL Server 2005, any ideas ?

drop table sales;

create table sales
(customer      varchar2(10),
 pos_type      varchar2(1),
qty      number(9),
 price      number(9)
)
;

insert into sales
values ('BOB','S','1','50.00')
;
insert into sales
values ('BOB','S','2','100.00')
;
insert into sales
values ('BOB','C','1','10.00')
;

select * from sales
;

CUSTOMER   P    QTY     PRICE
---------- - --------- ---------
BOB        S         1              50
BOB        S         2             100
BOB        C         1              10

When I run this sql it sums all the Pos Type S and takes away the SUM of Pos Type C

select      b.customer,
      sum(decode(b.pos_type,'S',b.qty,0)) - sum(decode(b.pos_type,'C',b.qty,0)),
      sum(decode(b.pos_type,'S',b.price,0)) - sum(decode(b.pos_type,'C',b.price,0))
from      sales b
group by b.customer
;

result that I need to get is:
CUSTOMER   QTY    RETAIL
---------- --------- ---------
BOB                2       140

Many Thanks,

Jason.
Start Free Trial
[+][-]05.15.2008 at 08:54AM PDT, ID: 21575007

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Server 2005
Tags: Microsoft, SQL Server, 2005
Sign Up Now!
Solution Provided By: dportas
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.20.2008 at 02:23AM PDT, ID: 21604513

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628