Solved

UPDATE Records and Increment Field Value  +1

Posted on 2001-07-10
3
644 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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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 …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 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