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 |
|
Rudys
Starting Member
8 Posts |
Posted - 2009-12-07 : 05:09:50
|
| Hi All.Can any body give me an advice/solution how to solved this condition?Table1 id amout1 100002 5000Table2id acc____amount status1 123456 5000 1 145678 30001 455789 20002 963258 60002 258741 6000result:--------Table2id acc___amount___Status1 123456 5000_______S ->(table1 (10000) - this record 5000)1 145678 3000_______S ->(table1 (10000 - previous rec 5000 - this record 3000)1 455789 3000_______F ->(table1 (10000 - previous rec 5000 - previous record 3000 - this record 3000) Minus2 963258 6000_______F ->Minus2 258741 6000_______F ->MinusPlease some help me to solved this problem because i already stuckTq |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-07 : 05:41:09
|
| How are we supposed to know which order to apply the running total?For example ID1 in table 2. Does the [acc] column control the ordering (bigger acc numbers are after smaller ones?)That doesn't work for Id2 in table 2 though.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-07 : 05:51:46
|
Here's one way to get the output you posted. However there are better ways. Your tables have meaningless names but I think I understand what you wanted/*Hi All.Can any body give me an advice/solution how to solved this condition?Table1 id amout1 100002 5000Table2id acc____amount status1 123456 5000 1 145678 30001 455789 20002 963258 60002 258741 6000result:--------Table2id acc___amount___Status1 123456 5000_______S ->(table1 (10000) - this record 5000)1 145678 3000_______S ->(table1 (10000 - previous rec 5000 - this record 3000)1 455789 3000_______F ->(table1 (10000 - previous rec 5000 - previous record 3000 - this record 3000) Minus2 963258 6000_______F ->Minus2 258741 6000_______F ->MinusPlease some help me to solved this problem because i already stuckTq*/DECLARE @table1 TABLE ( [Id] INT , [amount] MONEY )DECLARE @table2 TABLE ( [Id] INT , [acc] INT , [amount] MONEY )INSERT @table1 ([Id], [amount]) SELECT 1, 10000UNION SELECT 2, 5000INSERT @table2 ([Id], [acc], [amount]) SELECT 1, 123456, 5000UNION SELECT 1, 145678, 3000UNION SELECT 1, 455789, 3000UNION SELECT 2, 963258, 6000UNION SELECT 2, 258741, 6000;WITH ranks ( [Id] , [account] , [amount] , [position] )AS ( SELECT [ID] , [acc] , [amount] , ROW_NUMBER() OVER (PARTITION BY [ID] ORDER BY [acc] ASC) FROM @table2 ), runTot ( [Id] , [position] , [acc] , [amount] , [runTot] , [status] )AS ( SELECT r.[ID] , r.[position] , r.[account] , r.[amount] , t1.[amount] - r.[amount] , CASE WHEN t1.[amount] - r.[amount] >= 0 THEN 'S' ELSE 'F' END FROM ranks r JOIN @table1 t1 ON t1.[ID] = r.[ID] WHERE r.[position] = 1 UNION ALL SELECT r.[ID] , t.[position] , t.[account] , t.[amount] , r.[runTot] - t.[amount] , CASE WHEN r.[runTot] - t.[amount] >= 0 THEN 'S' ELSE 'F' END FROM ranks t JOIN runTot r ON r.[ID] = t.[ID] AND r.[position] = t.[position] - 1 )SELECT [Id] , [acc] , [amount] , [status]FROM runTotORDER BY [ID] , [acc] Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Rudys
Starting Member
8 Posts |
Posted - 2009-12-07 : 10:22:17
|
| tq Charlie i'will try the scrip, and for u'r question i orderring by accno,amout desctq 4 reply |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-08 : 02:15:45
|
With the help of Quirky update posted herehttp://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspxDECLARE @table1 TABLE ( [Id] INT , [amount] MONEY )DECLARE @table2 TABLE ( [Id] INT , [acc] INT , [amount] MONEY, total money )INSERT @table1 ([Id], [amount]) SELECT 1, 10000UNION SELECT 2, 5000INSERT @table2 ([Id], [acc], [amount]) SELECT 1, 123456, 5000UNION SELECT 1, 145678, 3000UNION SELECT 1, 455789, 3000UNION SELECT 2, 963258, 6000UNION SELECT 2, 258741, 6000declare @total money, @id intupdate @table2set total=@total,@total=case when @id=id then coalesce(@total,amount)+amount else amount end,@id=idselect t2.id,t2.acc,t2.amount, case when total<=t1.amount then 'S' else 'F' end as [status] from @table1 as t1 inner join @table2 as t2 on t1.id=t2.id MadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-08 : 06:15:35
|
| Quirky update = THE BLACK ARTS! (which I really should try and learn some day but.... man they are just wrong!)Slightly off topic but,is there any way in a quirky update to control the order. You cant use an order by for update statement or derived table....?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-08 : 07:30:12
|
quote: Originally posted by Transact Charlie Quirky update = THE BLACK ARTS! (which I really should try and learn some day but.... man they are just wrong!)Slightly off topic but,is there any way in a quirky update to control the order. You cant use an order by for update statement or derived table....?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
If the column has clustered index and with the help of index hint, it is gauranteed. For more informations refer thishttp://www.sqlservercentral.com/articles/T-SQL/68467/MadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-08 : 08:01:06
|
| Cheers.Time to get reading.......Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|
|