Question

I have prepared a sqlfiddle workspace which you can see the question and solve it easily. There is a table include ID, DAT, AMN, FLWC, FLWD, TYP. I would like to sort table by DAT and ID (DAT is first keyword in sort) then update FLWC and FLWD from previous record depend on TYP. for example if 0:previous record and 1:current record then:

 if typ1==d then (flwc1=flwc0 AND flwd1=flwd0+amn1)
 if typ1==c then (flwc1=flwc0+amn AND flwd1=flwd0)

You can see that flwc and flwd will be set to next record and one of them will be sum to AMN depend on TYP value.

Table before changes:

-- id__dat__amn__flwc__flwd__typ
-- 1   10   100  0     0     d
-- 2   11   200  0     0     c
-- 3   12   300  0     0     d
-- 4   13   400  0     0     c
-- 5   14   500  0     0     d
-- 6   15   600  0     0     c
-- 7   16   700  0     0     d

Table after UPDATE:

-- id__dat__amn__flwc__flwd__typ
-- 1   10   100  0     100   d
-- 2   11   200  200   100   c
-- 3   12   300  200   400   d
-- 4   13   400  600   400   c
-- 5   14   500  600   900   d
-- 6   15   600  1200  900   c
-- 7   16   700  1200  1600  d

Please note that in real test the DAT fields might be equal in several records because DAT means DATE. I have checked some answer from others but they don't have condition in their answers.

Live test: sqlfiddle

Was it helpful?

Solution

Complicated question but easy answer! (it shows that the start point is very important to thinking about a question)

declare @sumc decimal
declare @sumd decimal
set @sumc=0
set @sumd=0

update myTable set 
    @sumc+= case typ when 'c' then amn else 0 end, flwc=@sumc,
    @sumd+= case typ when 'd' then amn else 0 end, flwd=@sumd

OTHER TIPS

You should add two more column with bit values i.e. C and D. Value of C is one if typ is c and 0 if typ is d and in reverse for d.

for over i
{
    flwc(i)=flwc(i-1)+amn*C(i)
    flwd(i)=flwd(i-1)+amn*D(i)
}

---------------------------------- Edit ---------------------------------

This is pseudo-code of what you have to do. In SQL doing it requires some consecutive queries to be done.

In C#

//Initiation
SqlConnection Con=new SqlConnection(Some_Connection_String);
SqlCommand Comm=new SqlCommand("",Con);
Con.Open();
Comm.CommandText="SELECT TOP (1) id FROM table_name ORDER BY id DESC";
int MaxId=(int)Comm.ExecuteScaler();
int flwc=0,flwd=0;
int amn=0;
string typ="";

//Itteration
for (int Id=1;Id<MaxId;Id++)
{
    Comm.CommandText = "UPDATE table_name SET flwc="+(flwc+amn*(typ=="c"?1:0)).ToString()+",flwd="+(flwd+amn*(typ=="d"?1:0)).ToString()+" WHERE id="+Id.ToString();
    Comm.ExecuteNonQuery();

    //Update
    Comm.CommandText = "SELECT * FROM table_name WHERE id="+Id.ToString();
    reader=Comm.ExecuteReader();
    reader.Read();
    amn=reader.GetInt32(2);    
    flwc=reader.GetInt32(3);
    flwd=reader.GetInt32(4);
    typ=reader.GetString(5);
}
Con.Close();

If I have no syntax or logical errors (!) this should work fine.

If you want to update your table with sql in SQL Server you can do something like this:

i use two updates for your table and in the first one update the columns with amn and 0

with tab as
(select 
id,dat,amn,(select max(id) from mytable) as maxid,
case when typ = 'd' then 0 else amn end as flwc,
case when typ = 'c' then 0 else amn end as flwd,
typ,
0 as rn,0 as totalc,0 as totalD
from mytable)
update mytable set 
mytable.flwc = tab.flwc,mytable.flwd = tab.flwd
from mytable inner join tab on mytable.id = tab.id;

in the second one sum them:

with tab2 as
(select 
t1.id,t1.dat,x.d
from mytable t1
CROSS APPLY (SELECT SUM(v)
         FROM (  SELECT TOP(select count(*) from mytable) b.flwd
                 FROM mytable b
                 WHERE b.id <= t1.id
                 ORDER BY b.id DESC ) x(v)
          ) x(d)),

tab3 as
(select 
t1.id,t1.dat,x.c
from mytable t1
CROSS APPLY (SELECT SUM(v)
         FROM (  SELECT TOP(select count(*) from mytable) b.flwc
                 FROM mytable b
                 WHERE b.id <= t1.id
                 ORDER BY b.id DESC ) x(v)
          ) x(c)),
tab4 as (select tab2.id,tab2.dat,tab2.d,tab3.c from tab2,tab3 where tab2.id = tab3.id),
tab5 as (select t4.id,t4.dat,t4.amn,tab4.c as flwc,tab4.d as flwd,t4.typ 
from mytable t4,tab4
where t4.id = tab4.id)
update mytable set 
mytable.flwc = tab5.flwc,mytable.flwd = tab5.flwd
from mytable inner join tab5 on mytable.id = tab5.id;

SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top