Solved

Reorder ranking

Posted on 2013-05-13
2
333 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
[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 Comments
 
LVL 35

Accepted Solution

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

Open in new window

0
 

Author Comment

by:gogetsome
ID: 39162889
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
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…

690 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