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.
Author |
Topic |
pascal_jimi
Posting Yak Master
167 Posts |
Posted - 2013-12-10 : 02:40:40
|
declare @test table(id int not null identity(1,1),phone int,yanvar int,dekabr int,noyabr int,oktyabr int)insert @test(phone,yanvar,dekabr,noyabr,oktyabr)select 11,0,-2,-3,0union allselect 111,-1,0,-1,0union allselect 222,-1,-2,0,-5union allselect 333,-1,-2,-3,0union allselect 444,-1,-2,-3,-4select *from @testid phone yanvar dekabr noyabr oktyabr----------- ----------- ----------- ----------- ----------- -----------1 11 0 -2 -3 02 111 -1 0 -1 03 222 -1 -2 0 -54 333 -1 -2 -3 05 444 -1 -2 -3 -4it is the duty Monthly Phonesneed to calculate what numbers eats past debtsfor exampleNumber 11 no debtsNumber 111 -1$ 1 monthNumber 222 -1 + (-2) = -3 $ of debt 2 monthNumber 333 -1 + (-2 ) +( -3 )=-6 $ of debt 3 monthNumber 444 -1 + (-2) + (-3) + (-4) = -10 $ of debt 4 monthResults and fit in a tablephone 1 month 2 month 3 month 4 month111 -1 0 0 0222 0 -3 0 0333 0 0 -6 0444 0 0 0 -10http://sql-az.tr.gg/ |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-12-10 : 02:54:45
|
[code]select id, phone, [1 month] = case when yanvar <> 0 and dekabr = 0 then yanvar else 0 end, [2 month] = case when yanvar <> 0 and dekabr <> 0 and noyabr = 0 then yanvar + dekabr else 0 end, [3 month] = case when yanvar <> 0 and dekabr <> 0 and noyabr <> 0 and oktyabr = 0 then yanvar + dekabr + noyabr else 0 end, [4 month] = case when yanvar <> 0 and dekabr <> 0 and noyabr <> 0 and oktyabr <> 0 then yanvar + dekabr + noyabr + oktyabr else 0 endfrom @testwhere yanvar <> 0[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
pascal_jimi
Posting Yak Master
167 Posts |
Posted - 2013-12-10 : 02:57:23
|
thank very much khtanyou is wonderfulhttp://sql-az.tr.gg/ |
|
|
|
|
|
|
|