Solved

UPDATE Records and Increment Field Value  +1

Posted on 2001-07-10
3
638 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 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.

831 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