tags:

views:

305

answers:

3

My SQL table has 2 fields that impact my problem: "Priority" as real, and Start_Date as datetime. As the user adds items to the table, (s)he specifies the Priority for a given Start_Date.

If Item1 is Priority 1, then Item2 may be Priority 2 and it will be given a Start_Date after Item 1.

But if Item2 is given Priority 0.5 (or any number less than the Priority of Item1), then its Start_Date will be before Item 1's Start_Date.

After each Item is added, I want to go through the list of Items and Update all of the Priorities so they are integers, starting with the earliest Start_Date with a Priority =1, then 2, 3, etc.

I am working with SQL 2005. I know I can do this with a cursor. Is there a better way? How so?

Thank you.

A: 

You could do this with a temporary table. There might be better ways, but I can't think of one right now. This also assumes that you have a primary key on your table - if it's only start date and priority then this solution wouldn't be feasible.

create table #tempOrder (newPriority int identity,
                         id int)

insert into #tempOrder (id) select id from table order by Start_Date

update table set priority = newPriority 
  from #tempOrder
  where #tempOrder.id = table.id

One thing I don't understand with your problem is the mention of a priority of 0.5. You mention that priorities are assigned in order of start date, but also mention manually setting a priority to 0.5. When would this occur? What should be the behaviour if the start date of that priority conflicts with the ordering of priorities by start date?

Ryan Brunner
A new Item is inserted where its Priority is greater than all the ones earlier than it and less than all the ones greater than it.So a user might specify a priority of 3.5 and this Item would then be inserted into the table with a Start_Date = to the End_Date of the Item with the highest Priority less than 3.5. If there are no Items that have a priority < 3.5, it will be assigned the earliest Start_Date possible.
Melody Friedenthal
A: 
UPDATE mt
SET Priority = 
    (
     SELECT COUNT(*) 
     FROM MyTable mt2 
     WHERE mt2.Start_Date <= mt.Start_Date
    )
FROM MyTable mt
Joel Coehoorn
A: 

If all the startdatetimes are unique, Try this (inside of a Begin Trans so you can review the result and rollback if it's not what you want)

   Update Table Set 
     Priority = (Select Count(*) From table 
                 Where Start_Date <= T.Start_Date)
   From Table T
   Where Start_Date > getDate()

if they are not unique, you will get some rows with the same Priority, (where the DateTimes are the same) and some gaps in the Priority Sequences...

Charles Bretana
I am going to try your solution because I see now that there are some Items with identical Start_Dates. Items with identical Start_Dates start with identical priorities and should end up with identical priorities. Thank you! I will let you know if it works as needed.
Melody Friedenthal
Further enhancement: the Items being modified have to all have Start_Dates > Today. Do I add the additional where clause in the the subquery? For example:Update Table Set Priority = (Select Count(*) From table Where Start_Date <= T.Start_Date AndStart_Date >= Getdate()) From Table T
Melody Friedenthal
The numbering restarts with each Furnace. I successfully added a where clause for that. However, if two or more Items have the same Priority before the Update (and therefore start at the same time), they must have the same Priority after the Update. How do I do this?So far I have:Update tblTableSet Priority = (Select Count(*) From tblTable Where Start_Date <= T.Start_Date and convert(datetime, Start_Date, 101) >= @CutoffDate and Furnace_Name = T.Furnace_Name) From Table T
Melody Friedenthal
Yes, I edited to include that startdate must be in the future...
Charles Bretana
Did it!Update tblTableSet Priority = (Select Count(*) From tblTable Where Start_Date <= T.Start_Date and convert(datetime, Start_Date, 101) >= @CutoffDate and Furnace_Name = T.Furnace_Name and Priority <= T.Priority ) From tblTable T
Melody Friedenthal