SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select sum
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pascal_jimi
Posting Yak Master

Azerbaijan
150 Posts

Posted - 12/09/2013 :  06:50:12  Show Profile  Visit pascal_jimi's Homepage  Reply with Quote
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

India
52309 Posts

Posted - 12/09/2013 :  07:20:54  Show Profile  Reply with Quote
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

Azerbaijan
150 Posts

Posted - 12/09/2013 :  07:27:59  Show Profile  Visit pascal_jimi's Homepage  Reply with Quote
I need to sum negative numbers

http://sql-az.tr.gg/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000