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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update Record row by row

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 amout
1 10000
2 5000

Table2
id acc____amount status
1 123456 5000
1 145678 3000
1 455789 2000
2 963258 6000
2 258741 6000
result:
--------
Table2
id acc___amount___Status
1 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) Minus
2 963258 6000_______F ->Minus
2 258741 6000_______F ->Minus

Please some help me to solved this problem because i already stuck

Tq

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 amout
1 10000
2 5000

Table2
id acc____amount status
1 123456 5000
1 145678 3000
1 455789 2000
2 963258 6000
2 258741 6000
result:
--------
Table2
id acc___amount___Status
1 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) Minus
2 963258 6000_______F ->Minus
2 258741 6000_______F ->Minus

Please some help me to solved this problem because i already stuck

Tq
*/

DECLARE @table1 TABLE (
[Id] INT
, [amount] MONEY
)

DECLARE @table2 TABLE (
[Id] INT
, [acc] INT
, [amount] MONEY
)

INSERT @table1 ([Id], [amount])
SELECT 1, 10000
UNION SELECT 2, 5000

INSERT @table2 ([Id], [acc], [amount])
SELECT 1, 123456, 5000
UNION SELECT 1, 145678, 3000
UNION SELECT 1, 455789, 3000
UNION SELECT 2, 963258, 6000
UNION 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
runTot
ORDER BY
[ID]
, [acc]



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 desc

tq 4 reply
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-08 : 02:15:45
With the help of Quirky update posted here
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspx


DECLARE @table1 TABLE (
[Id] INT
, [amount] MONEY
)

DECLARE @table2 TABLE (
[Id] INT
, [acc] INT
, [amount] MONEY,
total money
)

INSERT @table1 ([Id], [amount])
SELECT 1, 10000
UNION SELECT 2, 5000

INSERT @table2 ([Id], [acc], [amount])
SELECT 1, 123456, 5000
UNION SELECT 1, 145678, 3000
UNION SELECT 1, 455789, 3000
UNION SELECT 2, 963258, 6000
UNION SELECT 2, 258741, 6000

declare @total money, @id int

update @table2
set total=@total,
@total=case when @id=id then coalesce(@total,amount)+amount else amount end,@id=id

select
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



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 1736
The 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 this
http://www.sqlservercentral.com/articles/T-SQL/68467/

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -