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
 General SQL Server Forums
 New to SQL Server Programming
 not getting proper output for (sum) function

Author  Topic 

under2811
Constraint Violating Yak Guru

366 Posts

Posted - 2010-04-16 : 05:29:37
Hi

I need your help plz

create table A
(id int,
name varchar(5),
fl varchar(1))

create table B
(id int,
Su int)

insert into A
values (1,'S','N')

insert into A
values (2,'M','Y')

insert into B
values (1,5)

insert into B
values (2,6)

insert into B
values (2,5)

insert into B
values (2,9)


select A.ID,(CASE WHEN A.[fl] = 'Y' THEN SUM(B.[Su]) ELSE B.[Su] END) AS [REV]
from A inner join B
ON A.id = B.id
group by A.id, A.fl, B.Su

what i am getting as below

ID REV
1 5
2 5
2 6
2 9

but need like

ID REV
1 5
2 20

Sachin.Nand

2937 Posts

Posted - 2010-04-16 : 05:34:42
[code]
select A.ID,SUM(CASE WHEN A.[fl] = 'Y' THEN (B.[Su]) ELSE B.[Su] END) AS [REV]
from A inner join B
ON A.id = B.id
group by A.id, A.fl

[/code]

PBUH
Go to Top of Page

under2811
Constraint Violating Yak Guru

366 Posts

Posted - 2010-04-16 : 05:51:24
Thanks A LOt... )
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-16 : 06:23:09
quote:
Originally posted by under2811

Thanks A LOt... )



You are welcome.

PBUH
Go to Top of Page
   

- Advertisement -