| Author |
Topic |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-12-28 : 00:28:16
|
| I have two tables tblFunds and tblTranstblFundsID-----FundAmount----------------------------01-----10000tblTrans----------------------------trID-----trTransAmt0000-----00001-----500002-----1000003-----2000004-----150I want to see this result...trID-----trTransAmt-----Balance0000-----0--------------100000001-----50-------------99500002-----100------------98500003-----200------------96500004-----150------------9500Balance result is trTransAmt deducted from FundAmount.How?Want Philippines to become 1st World COuntry? Go for World War 3... |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-12-28 : 00:36:53
|
| select t.trID, t.trTransAmt, f.FundAmount - (select sum(trTransAmt) from tblTrans t2 where t2.trID <= t.trID)fromtblTrans tcross join tblFunds fI'm guessing you will have an account id somewhere though.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-12-28 : 00:55:53
|
| -- tblFunds-- ID-----FundAmount-- ------------------------------ 01-----10000-- -- -- tblTrans-- ------------------------------ trID-----trTransAmt-- 0000-----0-- 0001-----50-- 0002-----100-- 0003-----200-- 0004-----150-- -- -- I want to see this result...-- -- trID-----trTransAmt-----Balance-- 0000-----0--------------10000-- 0001-----50-------------9950-- 0002-----100------------9850-- 0003-----200------------9650-- 0004-----150------------9500DECLARE @tblFunds TABLE( ID CHAR(2), FundAmount MONEY) --Why are you using CHAR for you ID columns?DECLARE @tbltrans TABLE( trID CHAR(4), trTransAmt MONEY)INSERT @tblFunds(ID, FundAmount) SELECT '01',10000INSERT @tblTrans(trID, trTransAmt) SELECT '0000',0 UNION ALL SELECT '0001',50 UNION ALL SELECT '0002',100 UNION ALL SELECT '0003',200 UNION ALL SELECT '0004',150SELECT tt1.trID, tt1.trTransAmt, tf.FundAmount - ( SELECT SUM(trTransAmt) FROM @tblTrans WHERE trID <= tt1.trID) AS BalanceFROM @tblTrans tt1 CROSS JOIN @tblFunds tfMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-12-28 : 03:01:40
|
| Is there possible way of not using a Select statement? Can we use variable to do this?Let say...Select @trTransAmt = (@trTransAmt + trTransAmt), FundAmount - @trTransAmtsomething like that?Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-12-28 : 17:52:37
|
| That doesn't make sense.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-12-28 : 17:57:33
|
| trID-----trTransAmt-----Balance0000-----0--------------100000001-----50-------------99500002-----100------------98500003-----200------------96500004-----150------------9500How can you get that in a variable?You can't set a variable and return a resultset in the same statement.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-12-28 : 17:58:09
|
In other words:That doesn't make sense. MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-12-28 : 18:02:36
|
| Yep - just pointing out why.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-12-29 : 00:27:13
|
| How about this...tblFundsID-----FundAmount----------------------------AA-----10000tblTrans----------------------------trID-----trTransAmtAAAA-----0AAAB-----50AAAC-----100AAAD-----200AAAE-----150I want to see this result...trID-----trTransAmt-----BalanceAAAA-----0--------------10000AAAB-----50-------------9950AAAC-----100------------9850AAAD-----200------------9650AAAE-----150------------9500ID's are alpha formated... How?Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-12-29 : 01:28:28
|
| That doesn't make a difference - the query still works.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-12-30 : 07:07:38
|
| Just a follow up!How about without those ID's? How?Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-12-30 : 12:38:00
|
| We don't see what the problem is with the ID's. Aren't you being a little bit lazy here Jonas? All you have to do is run it like we would:[code]-- tblFunds-- ID-----FundAmount-- ------------------------------ 01-----10000-- -- -- tblTrans-- ------------------------------ trID-----trTransAmt-- 0000-----0-- 0001-----50-- 0002-----100-- 0003-----200-- 0004-----150-- -- -- I want to see this result...-- -- trID-----trTransAmt-----Balance-- 0000-----0--------------10000-- 0001-----50-------------9950-- 0002-----100------------9850-- 0003-----200------------9650-- 0004-----150------------9500DECLARE @tblFunds TABLE(ID CHAR(2), FundAmount MONEY) --Why are you using CHAR for you ID columns?DECLARE @tbltrans TABLE(trID CHAR(4), trTransAmt MONEY)INSERT @tblFunds(ID, FundAmount)SELECT '01',10000INSERT @tblTrans(trID, trTransAmt)SELECT 'AAAA',0 UNION ALLSELECT 'AAAB',50 UNION ALLSELECT 'AAAC',100 UNION ALLSELECT 'AAAD',200 UNION ALLSELECT 'AAAE',150SELECTtt1.trID,tt1.trTransAmt,tf.FundAmount - (SELECT SUM(trTransAmt)FROM @tblTrans WHERE trID <= tt1.trID) AS BalanceFROM@tblTrans tt1CROSS JOIN @tblFunds tfRESULTS:AAAA .0000 10000.0000AAAB 50.0000 9950.0000AAAC 100.0000 9850.0000AAAD 200.0000 9650.0000AAAE 150.0000 9500.0000[/CODE]MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2005-01-01 : 21:33:17
|
| Nope! derrickleggett... I just hardly practicing SQL... :DWant Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-01-02 : 12:39:10
|
| :) Okay. Was that what you were looking for?MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|