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 |
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-09-04 : 02:16:51
|
I have this data...SELECT x.*FROM ( SELECT '2005-03-30 14:14:23.903' AS dtTime, 'S2005CT001' as ItemCode, 30 as QTYin, NULL as QTYout union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', 10 as QTYin, NULL as QTYout union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', NULL as QTYin, 15 as QTYout union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', 22 as QTYin, NULL as QTYout union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', NULL as QTYin, 10 as QTYout ) AS x I want to have a running balance by ItemCode with the given data above.Result shoul be like this...SELECT x.*FROM ( SELECT '2005-03-30 14:14:23.903' AS dtTime, 'S2005CT001' as ItemCode, 30 as QTYin, NULL as QTYout, 0 as Beg, 30 as Ending union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', 10 as QTYin, NULL as QTYout, 30, 40 as Ending union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', NULL as QTYin, 15 as QTYout, 40, 25 union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', 22 as QTYin, NULL as QTYout, 25, 47 union all SELECT '2005-04-01 14:14:23.903' AS dtTime, 'S2005CT001', NULL as QTYin, 10 as QTYout, 47, 37 ) AS x Thanks in advance.Want Philippines to become 1st World COuntry? Go for World War 3... |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-04 : 02:32:04
|
Do you have primary key in the table ? KH |
 |
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2006-09-04 : 02:35:11
|
It is possible, but can you post the design of your source table?Normally for this sort of thing you would do something like this:SELECT t.ID, t.QtyIn, t.QtyOut, (SELECT SUM(QtyIn+QtyOut) FROM myTable WHERE ID <= t.ID) as TotalFROM myTable tORDER BY ID ASC |
 |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-09-04 : 02:37:50
|
quote: Originally posted by khtan Do you have primary key in the table ? KH
Yup! sID is my Primary KeyAny approaches besides Timmy's post?TNXWant Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-04 : 03:02:04
|
Why don't you tell us this at the first posting? The sID column is not even mentioned?declare @test table (sid int, dtTime datetime, ItemCode varchar(20), QTYin int, QTYout int)insert @testselect 1, '2005-03-30 14:14:23.903', 'S2005CT001', 30, NULLunion allSELECT 3, '2005-04-01 14:14:23.903', 'S2005CT001', 10, NULLunion allSELECT 4, '2005-04-01 14:14:23.903', 'S2005CT001', NULL, 15union allSELECT 20, '2005-04-01 14:14:23.903', 'S2005CT001', 22, NULLunion allSELECT 25, '2005-04-01 14:14:23.903', 'S2005CT001', NULL, 10SELECT t.*, (select isnull(sum(t2.qtyin), 0) - isnull(SUM(t2.qtyout), 0) from @test t2 where t2.sid < t.sid) Beg, (select isnull(sum(t2.qtyin), 0) - isnull(SUM(t2.qtyout), 0) from @test t2 where t2.sid <= t.sid) EndingFROM @test t Peter LarssonHelsingborg, Sweden |
 |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-09-04 : 03:05:41
|
Any approach? seems my query is very much slow.Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-04 : 12:01:26
|
If you use reports, make use of Running Total featureMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|