December 16, 2009 at 6:09 am
Bhavesh_Patel (12/16/2009)
But in order to insert the new city at SortOrder 4, the subsequent SortOrder also needs to be changed. This is the problem.Is there a simple way, which will allow us to make changes in SortedCities table?
Uh-huh... and what kind of problem would that pose in a CASE statement in the ORDER BY?
Start thinking... if you want to "insert" a new city in the middle of all the other cities, how should it be done as a human would do it? The answer is, add "1" to all of the numbers >= to the number you want to insert and then insert the new row. For practice, you get to write the code to do that. 😀 It should be general purpose where the only thing you pass to the script or proc is the name of the new city and the sort order number you want it to have.
Think, man, think.... you're going on an interview. They're going to ask simple but thoughtful questions like this. If you can't think in such a manner, the interview will not go well because they need someone who can think of such simple things as this. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
December 16, 2009 at 6:15 am
Once you get done with the code for a new city, write some code to change the sort order of a city. Think and keep it simple... no need for complexity here.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 16, 2009 at 6:25 am
Jeff Moden (12/16/2009)
Bhavesh_Patel (12/16/2009)
But in order to insert the new city at SortOrder 4, the subsequent SortOrder also needs to be changed. This is the problem.Is there a simple way, which will allow us to make changes in SortedCities table?
Uh-huh... and what kind of problem would that pose in a CASE statement in the ORDER BY?
You know, I can think of a solution that combines CASE and UNION and requires no modification when a new city is added (unless it's added to the specially ordered cities)
Actual code left as an exercise for the reader. 😉
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 16, 2009 at 6:26 am
Yes, I get it now. I shall create a proc which will update the SortedCities table.
Thanks Jeff, it was really a great help.
December 16, 2009 at 8:02 am
Bhavesh_Patel (12/16/2009)
Yes, I get it now. I shall create a proc which will update the SortedCities table.Thanks Jeff, it was really a great help.
Bhavesh
Thanks for taking it the right way, Bhavesh.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply