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 |
|
pssumesh2003
Starting Member
35 Posts |
Posted - 2009-10-14 : 00:54:04
|
| Hi every bodyI am a new in sql server 2005. in my project i have 2 columns deposit,withdrawal. i want to insert into another tabledeposit,withdrawal,balance(2000) using select statementi want set balance in a way thatvalue of balance in previous row + value of deposit in same row Deposit - value of deposit in same row Withdralplease give the sample query |
|
|
sanoj_av
Posting Yak Master
118 Posts |
Posted - 2009-10-14 : 01:29:29
|
| post the complete table Structure. How do you find the previous row? by transaction date? which balance will you take if there are more than one transaction on a single date?. probably there will be one more column like VoucherNo/InvoiceNo.etc. |
 |
|
|
pssumesh2003
Starting Member
35 Posts |
Posted - 2009-10-14 : 02:21:31
|
| Hi Sanoji have 2 columns deposit,withdrawal. i want to insert into another temp tabledeposit,withdrawal,balance(2000) using select statementI try insert into #newtable(deposit,withdrawal,balance) select deposit,withdrawal,balance+deposit-withdrawal.but it did'nt give the desierd outputi want set balance in a way thatvalue of balance in previous row + value of deposit in same row Deposit - value of deposit in same row Withdralplease give the sample query |
 |
|
|
sanoj_av
Posting Yak Master
118 Posts |
Posted - 2009-10-14 : 02:41:24
|
| Deposit Withdrawal Balance100 50 50200 25 225125 200 150150 50 250For example,how do you take the Previous Balance of the 3rd record? Will it be 225? If yes, you are relying on the defaul order on which SQL server sorts the record.The Order is not guaranteed on a heap table when you query it next time unless you specify a sort column. |
 |
|
|
pssumesh2003
Starting Member
35 Posts |
Posted - 2009-10-14 : 02:49:39
|
| dear Sanojyes, i want out put like that in temp table.but how to insert into that table ? |
 |
|
|
Kabila
Starting Member
33 Posts |
Posted - 2009-10-14 : 03:14:58
|
| First You move table output into #tmp table @ what order you whatcreate table #tmp(id int identity(1,1),deposit numeric(9,2),withdrawal numeric(9,2))insert into #tmp(deposit,withdrawal)select 5000,2000union allselect 1000,2000union allselect 2000,1000union allselect 3000,1000with cte as(select top 1 id, deposit,withdrawal ,cast(deposit-withdrawal as numeric(9,2)) as balance from #tmp order by idunion allSelect a.id,a.deposit,a.withdrawal, cast(balance+a.deposit-a.withdrawal as numeric(9,2)) as balancefrom #tmp a ,cte bwhere a.id=b.id+1) select deposit,withdrawal ,deposit,withdrawal from cte; |
 |
|
|
sanoj_av
Posting Yak Master
118 Posts |
Posted - 2009-10-14 : 03:23:44
|
| create Table #AccTrans(deposit money,withdrawal money)Insert into #AccTransSELECT 100,50 UNION ALLSELECT 200,25 UNION ALLSELECT 125,200 UNION ALLSELECT 150,50 create Table #AccBal(deposit money,withdrawal money,Balance Money)goWith CTE AS (Select row_number() over(order by (select 1)) as SrNo,deposit,withdrawal from #AccTrans)insert into #AccBal(deposit,withdrawal,Balance)Select deposit, withdrawal, (Select sum(deposit) from CTE B Where B.srNo<=A.srNo)-(Select sum(withdrawal) from CTE B Where B.srNo<=A.srNo) AS BalFrom CTE ASelect * from #AccTransSelect * from #AccBal |
 |
|
|
|
|
|
|
|