Solved

Reorder ranking

Posted on 2013-05-13
2
327 Views
Last Modified: 2013-05-13
Hello I have the following table and data below. How can I reorder the rank column if say the product with the 5 rank is deleted. An update statement to reorder the rank once an element has been deleted? How would you reorder the rank when the rank must stay 1 through 8 after the delete?

Id               pId      type       rank
68      293552      DVD      1
66      297906      DVD      2
65      298007      DVD      3
67      283870      DVD      4
69      285618      DVD      5
70      293129      DVD      6
71      288795      DVD      7
74      288723      DVD      8
73      289583      DVD      9
0
Comment
Question by:gogetsome
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
How about:
Update YourTable set rank = rank - 1 Where rank > 5

Open in new window

0
 

Author Comment

by:gogetsome
Comment Utility
Exactly! Thanks for your help!


This is what I came up with that works perfectly:

      @TopSellersId int
      
      AS

      Declare @Rank int
      Set @Rank = (Select [Rank] From TopSellers Where TopSellersId = @TopSellersId)
      Declare @Type Varchar(20)
      Set @Type = (Select [Type] From TopSellers Where TopSellersId = @TopSellersId)


Delete TopSellers Where TopSellersId = @TopSellersId

Update TopSellers Set [Rank] = ([Rank] - 1) Where [rank] > @Rank and [Type] = @Type
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.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now