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
 select sum

Author  Topic 

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2013-12-09 : 06:50:12
declare @test table
(id int not null identity(1,1),
phone int,
one int,
two int,
three int,
four int
)

insert @test
(phone,one,two,three,four)
select
11,0,-2,-3,0
union all
select
111,-1,0,-1,0
union all
select
222,-1,-2,0,-5
union all
select
333,-1,-2,-3,0
union all
select
444,-1,-2,-3,-4


select *from @test


id phone one two three four
----------- ----------- ----------- ----------- ----------- -----------
1 11 0 -2 -3 0
2 111 -1 0 -1 0
3 222 -1 -2 0 -5
4 333 -1 -2 -3 0
5 444 -1 -2 -3 -4


if one time
one <0 then
m1=sum(one),m2=0,m3=0,m4=0

if one time
one <0 and two <0 then
m1=0,m2=sum(one+two),m3=0,m4=0

if one time
one <0 and two <0 and three<0 then
m1=0 ,m2=0, m3=sum(one+two+three),m4=0

if one time
one <0 and two <0 and three<0 and four<0 then sum(one+two+three+four)


How can I get so Results


phone m1 m2 m3 m4
----------- ----------- ------- --------- -----------

111 -1 0 -1 0
222 0 -3 0 0
333 0 0 -6 0
444 0 0 0 -10









http://sql-az.tr.gg/

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-09 : 07:20:54
Sorry I dont get your rules. Can you elaborate?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2013-12-09 : 07:27:59
I need to sum negative numbers

http://sql-az.tr.gg/
Go to Top of Page
   

- Advertisement -