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 |
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-12 : 04:42:38
|
I have a table as follow:Date In OutDec 1 5 0Dec 2 0 3Dec 2 4 0Dec 3 0 2How to add a result field and the table become:Date In Out ResultDec 1 5 0 5Dec 2 0 3 2Dec 2 4 0 6Dec 3 0 2 4Thanks |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-12 : 05:02:26
|
What is the criteria for Result column--Chandu |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-12 : 05:09:39
|
Something likewith cte as(select *, seq = row_number() over (order by date, in, out) from tbl) ,cte2 as(select date, in, out, result = in-out, seq from cte where seq = 1union allselect cte2.date, cte2.in, cte2.out, result = cte.Result + cte2.in-cte2.out, cte2.seq from cte join cte2 on cte2.seq = cte.seq + 1)select *from cte2order by seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-12 : 05:47:04
|
Result column are:Row 1 : in-out :5-0= 5 result row = 5Row 2 : in-out :0-3=-3 result row = 5(Result row 1)+ (-3)= 2Row 3 : in-out :4-0= 4 Result row = 2(Result row 2)+ 4 = 6Row 4 : in-out :0-2=-2 result row = 6(result row 3) + (-2) = 4quote: Originally posted by bandi What is the criteria for Result column--Chandu
|
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-12 : 06:13:14
|
sorry I post in the wrong forum, my sql server is (2005)quote: Originally posted by nigelrivett Something likewith cte as(select *, seq = row_number() over (order by date, in, out) from tbl) ,cte2 as(select date, in, out, result = in-out, seq from cte where seq = 1union allselect cte2.date, cte2.in, cte2.out, result = cte.Result + cte2.in-cte2.out, cte2.seq from cte join cte2 on cte2.seq = cte.seq + 1)select *from cte2order by seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-12 : 07:03:30
|
declare @t table (dte datetime, inp int, outp int)insert @t select '20121201', 5, 0insert @t select '20121202', 0, 3insert @t select '20121202', 4, 0insert @t select '20121203', 0, 2;with cte as (select *, val = convert(varchar(8),dte,112) + right('0000' + convert(varchar(4),inp),4) + right('0000' + convert(varchar(4),outp),4)from @t),cte2 as (select *, seq=(select count(*) from cte t2 where t2.val <= t1.val)from cte t1) --select * from cte2, cte3 as(select dte, inp, outp, result = inp-outp, seq from cte2 where seq = 1union allselect cte3.dte, cte3.inp, cte3.outp, result = cte3.Result + cte2.inp-cte2.outp, cte3.seq+1 from cte2 join cte3 on cte2.seq = cte3.seq + 1)select *from cte3order by seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-12 : 07:42:05
|
Thanks for your prompt reply, I have copy your code and paste to my visual studio query and run it, it return with error.quote: Originally posted by nigelrivett declare @t table (dte datetime, inp int, outp int)insert @t select '20121201', 5, 0insert @t select '20121202', 0, 3insert @t select '20121202', 4, 0insert @t select '20121203', 0, 2;with cte as (select *, val = convert(varchar(8),dte,112) + right('0000' + convert(varchar(4),inp),4) + right('0000' + convert(varchar(4),outp),4)from @t),cte2 as (select *, seq=(select count(*) from cte t2 where t2.val <= t1.val)from cte t1) --select * from cte2, cte3 as(select dte, inp, outp, result = inp-outp, seq from cte2 where seq = 1union allselect cte3.dte, cte3.inp, cte3.outp, result = cte3.Result + cte2.inp-cte2.outp, cte3.seq+1 from cte2 join cte3 on cte2.seq = cte3.seq + 1)select *from cte3order by seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-12 : 07:50:43
|
quote: Originally posted by nigelrivett
Hi nigel,Result column is fine, but not other columnsdte inp outp result seq2012-12-01 00:00:00.000 5 0 5 12012-12-01 00:00:00.000 5 0 2 22012-12-01 00:00:00.000 5 0 6 32012-12-01 00:00:00.000 5 0 4 4----------------------------------------------------------------------Small modification in your query..declare @t table (dte datetime, inp int, outp int)insert @t select '20121201', 5, 0insert @t select '20121202', 0, 3insert @t select '20121202', 4, 0insert @t select '20121203', 0, 2;with cte as (select *, ROW_NUMBER() OVER( ORDER BY dte, inp, outp) Valfrom @t),cte2 as (select *, seq=(select count(*) from cte t2 where t2.val <= t1.val)from cte t1) --select * from cte2, cte3 as(select dte, inp, outp, result = inp-outp, seq from cte2 where seq = 1union allselect cte3.dte, cte3.inp, cte3.outp, result = cte3.Result + cte2.inp-cte2.outp, cte3.seq+1 from cte2 join cte3 on cte2.seq = cte3.seq + 1)select cte.dte, cte.inp, cte.outp, cte3.resultfrom cte3 JOIN cte ON cte3.seq = cte.Valorder by seq --Chandu |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-12 : 07:53:41
|
Hi Patrick,Nigel's code is compatible with MSSQL 2005 also. Which error you are getting? Post error message--Chandu |
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-13 : 05:51:08
|
Thanks BandiI copy your code and paste to my sql query:declare @t table (dte datetime, inp int, outp int)insert @t select '20121201', 5, 0insert @t select '20121202', 0, 3insert @t select '20121202', 4, 0insert @t select '20121203', 0, 2;with cte as (select *, ROW_NUMBER() OVER( ORDER BY dte, inp, outp) Valfrom @t),cte2 as (select *, seq=(select count(*) from cte t2 where t2.val <= t1.val)from cte t1) --select * from cte2, cte3 as(select dte, inp, outp, result = inp-outp, seq from cte2 where seq = 1union allselect cte3.dte, cte3.inp, cte3.outp, result = cte3.Result + cte2.inp-cte2.outp, cte3.seq+1 from cte2 join cte3 on cte2.seq = cte3.seq + 1)select cte.dte, cte.inp, cte.outp, cte3.resultfrom cte3 JOIN cte ON cte3.seq = cte.Valorder by seqReply as Follow:Msg 156, Level 15, State 1, Line 6Incorrect syntax near the keyword 'with'.Msg 195, Level 15, State 10, Line 7'ROW_NUMBER' is not a recognized function name.Msg 170, Level 15, State 1, Line 15Line 15: Incorrect syntax near ','.quote: Originally posted by bandi Hi Patrick,Nigel's code is compatible with MSSQL 2005 also. Which error you are getting? Post error message--Chandu
|
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-13 : 05:57:53
|
Which version of sql server are you running.Is it running in compatability mode?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-13 : 06:25:35
|
run this one to know compatibility levelEXEC sp_dbcmptlevel 'YourDBName' After that sp_dbcmptlevelSets certain database behaviors to be compatible with the specified version of SQL Server.Example:----SQL Server 2005 database compatible level to SQL Server 2000EXEC sp_dbcmptlevel AdventureWorks, 80;GO----SQL Server 2000 database compatible level to SQL Server 2005EXEC sp_dbcmptlevel AdventureWorks, 90;GONote:Version of SQL Server database can be one of the following:60 = SQL Server 6.065 = SQL Server 6.570 = SQL Server 7.080 = SQL Server 200090 = SQL Server 2005--Chandu |
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-13 : 09:08:53
|
Thank You Bandi,Sorry ! My sql server is 8.0.760.0 = SQL Server 2000 SP3 / SP3a 1 (Updated 2003-08-27) quote: Originally posted by bandi run this one to know compatibility levelEXEC sp_dbcmptlevel 'YourDBName' After that sp_dbcmptlevelSets certain database behaviors to be compatible with the specified version of SQL Server.Example:----SQL Server 2005 database compatible level to SQL Server 2000EXEC sp_dbcmptlevel AdventureWorks, 80;GO----SQL Server 2000 database compatible level to SQL Server 2005EXEC sp_dbcmptlevel AdventureWorks, 90;GONote:Version of SQL Server database can be one of the following:60 = SQL Server 6.065 = SQL Server 6.570 = SQL Server 7.080 = SQL Server 200090 = SQL Server 2005--Chandu
|
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-13 : 17:11:25
|
[code]declare @t table (dte datetime, inp int, outp int)insert @t select '20121201', 5, 0insert @t select '20121202', 0, 3insert @t select '20121202', 4, 0insert @t select '20121203', 0, 2Select ID = Identity(int,1,1) , qty = inp - outp,* into #Tfrom @T tselect t.*,(select sum(qty) from #T tt where tt.ID < = t.Id )from #T t[/code] |
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-13 : 23:08:44
|
Well Done! Sodeep, thank you very much, I still need some time to figure out your code.how if my existing table name TEST, with dataDATE inp outp 2012-12-01 5 0 2012-12-02 0 3 2012-12-02 4 0 2012-12-03 0 2 After running your code ,It will create a New table TEST-1 with DataID QTY DATE inp outp Remain1 5 2012-12-01 5 0 52 -3 2012-12-02 0 3 23 4 2012-12-02 4 0 64 -2 2012-12-03 0 2 4Thank you againquote: Originally posted by sodeep
declare @t table (dte datetime, inp int, outp int)insert @t select '20121201', 5, 0insert @t select '20121202', 0, 3insert @t select '20121202', 4, 0insert @t select '20121203', 0, 2Select ID = Identity(int,1,1) , qty = inp - outp,* into #Tfrom @T tselect t.*,(select sum(qty) from #T tt where tt.ID < = t.Id )from #T t
|
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-14 : 10:05:36
|
cool...you are welcome |
|
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-12-17 : 00:10:09
|
Anyone can help ! IF the table has a field name code:code dte inp outp S1000 2012-12-01 5 0 51000 2012-12-02 0 3 -31000 2012-12-02 4 0 41000 2012-12-03 0 2 -22000 2012-12-02 6 0 62000 2012-12-03 0 4 -43000 2012-12-02 7 0 73000 2012-12-02 0 4 -4the outout become :code dte inp outp S balance1000 2012-12-01 5 0 5 51000 2012-12-02 0 3 -3 21000 2012-12-02 4 0 4 61000 2012-12-03 0 2 -2 42000 2012-12-02 6 0 6 62000 2012-12-03 0 4 -4 23000 2012-12-02 7 0 7 73000 2012-12-02 0 4 -4 3 quote: Originally posted by sodeep cool...you are welcome
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-12-17 : 01:02:46
|
quote: Originally posted by patrickjao Anyone can help ! IF the table has a field name code:code dte inp outp S1000 2012-12-01 5 0 51000 2012-12-02 0 3 -31000 2012-12-02 4 0 41000 2012-12-03 0 2 -22000 2012-12-02 6 0 62000 2012-12-03 0 4 -43000 2012-12-02 7 0 73000 2012-12-02 0 4 -4the outout become :code dte inp outp S balance1000 2012-12-01 5 0 5 51000 2012-12-02 0 3 -3 21000 2012-12-02 4 0 4 61000 2012-12-03 0 2 -2 42000 2012-12-02 6 0 6 62000 2012-12-03 0 4 -4 23000 2012-12-02 7 0 7 73000 2012-12-02 0 4 -4 3 quote: Originally posted by sodeep cool...you are welcome
Is that not what you wanted ? KH[spoiler]Time is always against us[/spoiler] |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-17 : 08:23:27
|
Do thisdeclare @t table (code int,dte datetime, inp int, outp int)insert @t select 1000,'20121201', 5, 0insert @t select 1000,'20121202', 0, 3insert @t select 1000,'20121202', 4, 0insert @t select 1000,'20121203', 0, 2insert @t select 2000,'2012-12-02',6,0insert @t select 2000,'2012-12-03',0,4insert @t select 3000,'2012-12-02',7,0insert @t select 3000,'2012-12-02',0,4Select ID = Identity(int,1,1) , qty = inp - outp,* into #Tfrom @T tselect t.*,(select sum(qty) from #T tt Where tt.code = t.code and tt.ID < = t.ID )from #T t |
|
|
|
|
|
|
|