T-SQL Updating a Field in the Last and Next to Last Rows

Hello:

I need to update the "percentage" field of the last and next to last rows of table GL00103.  The last row needs to be 20, and the next to last row needs to be 15.

Please help!  Below are two queries.  I need to know what to put in the clause of the first query that says "where the row is the last row" and in the second query that says "where the row is the next to the last row":

UPDATE GL00103 SET (SELECT (GL00103.PRCNTAGE) FROM GL00103 'where the row is the last row') = 20
from GL00103
INNER JOIN GL00100 ON GL00103.ACTINDX = GL00100.ACTINDX
where GL00100.ACCTTYPE = '3' and GL00100.FXDORVAR = '1' AND GL00100.ACTNUMBR_2 = '6190'

UPDATE GL00103 SET (SELECT (GL00103.PRCNTAGE) FROM GL00103 'where the row is the next to the last row') = 15
from GL00103
INNER JOIN GL00100 ON GL00103.ACTINDX = GL00100.ACTINDX
where GL00100.ACCTTYPE = '3' and GL00100.FXDORVAR = '1' AND GL00100.ACTNUMBR_2 = '6190'

Thanks!  Much appreciated!

TBSupport
LVL 1
TBSupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
in sql server 2005+, you can do this "much easier", using CTE and ROW_NUMBER() ...
here is the "draft" code, all you need to fill in is the ...  to specify the order to dermine "last row" and "next to last row". a typical example would be ORDER BY some_date_time_field DESC

WITH Updates
AS
( SELECT GL00103.PRCNTAGE
        , ROW_NUMBER() OVER ( ORDER BY .... ) rn
     FROM GL00103
      JOIN GL00100 
            ON GL00103.ACTINDX = GL00100.ACTINDX
      WHERE GL00100.ACCTTYPE = '3' 
          AND GL00100.FXDORVAR = '1' 
          AND GL00100.ACTNUMBR_2 = '6190'
)
UPDATE Updates
   SET PRCNTAGE = CASE rn WHEN 1 THEN 20 WHEN 2 THEN 15 ELSE PRCNTAGE END
WHERE rn <= 2 ; 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TBSupportAuthor Commented:
Thanks, angelIII!!!!!

That worked.  The only thing that I did slightly different was that I put in (for the "ORDER BY") Order by GL00103.DEX_ROW_ID DESC.

Thanks, again!

TBSupport
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.