Solved

UPDATE Records and Increment Field Value  +1

Posted on 2001-07-10
3
652 Views
Last Modified: 2012-06-21
Here is my SQL query:


update ##tmp_results
set StratLevel = 3
where prov_id  in (select TOP 50 PERCENT Prov_ID FROM ##tmp_results ORDER BY ScoreOverall DESC)


Basically, it is dumping results that meet the search criteria into a ##global_temp table.

I would like to keep the functionality of this query intact AND be able to add an incremented value (+1 for each row updated) to a field (In the example below, "Rank" would be the field containing the incremented values).

Example:

ScoreOverall        Prov_ID           Rank
------------        -------           ----
100                 Gary                1
 87                 Bob                 2
 85                 Neil                3
 79                 Jack                4


Is there a way I can add this funtionality to the existing query?

I am trying to stay away from using a cursor to accomplish this procedure -- they are PAINFULLY slow!

0
Comment
Question by:CArnold
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Author Comment

by:CArnold
ID: 6271827
I have upped the ante to 200 points.
Any responses?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 6271979
Try this:

update ##tmp_results
set StratLevel = 3,
Rank = 1+(SELECT count(*) FROM ##tmp_results i WHERE i.ScoreOverall < ##tmp_results.ScoreOverall)
where prov_id  in (select TOP 50 PERCENT Prov_ID FROM ##tmp_results ORDER BY ScoreOverall DESC)

Cheers
0
 
LVL 1

Author Comment

by:CArnold
ID: 6273181
Works great!

Well... almost.  I had to turn your '<' sign around to make it work properly.  I never thought about doing it that way.

Great answer.  The points are yours!

Thanks,
Charles
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question