Guest User

Untitled

a guest
Oct 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. Declare @counter int, @unit char(4),@unit2 char(4),@days int, @totalDays int
  2. --initialize to 0
  3. Select @days=0, @totalDays=0
  4.  
  5. /*Start at the current row*/
  6. SELECT @counter = (SELECT max(rownum) FROM #mixer)
  7.  
  8. While @counter > 0
  9.  
  10. begin
  11.  
  12.  
  13. /*initialize unit and days variables to first value*/
  14. SET @unit =(SELECT unit FROM #mixer WHERE rownum = @counter),
  15. @unit2 =(SELECT unit FROM #mixer WHERE rownum = @counter-1),
  16. @days= (SELECT days FROM #mixer WHERE rownum = @counter)
  17.  
  18.  
  19. /*compare to see if the current cell is the same as the previous one*/
  20. IF compare(@unit,@unit2)=0
  21. begin
  22. /*If they are equal increment total variable by # of days*/
  23. SET @totalDays = @totalDays + @days
  24.  
  25. /*Move up one row*/
  26. set @counter= @counter-1
  27. end
  28.  
  29. ELSE /*We are done..previous row had a different group*/
  30.  
  31. /*Persistance of values*/
  32.  
  33. INSERT INTO #totals
  34. SELECT distinct unique_id,
  35. another_id,
  36. @unit,
  37. @days-1,
  38. @totalDays
  39. FROM #mixer
  40.  
  41.  
  42. /*needs to be reset to avoid infinite loop*/
  43. SET @counter = 0, @totalDays = 0
  44.  
  45. end
  46. /*Get rid of rows we just used in #mixer*/
  47. DELETE #mixer WHERE unique_id = @unique_id
Add Comment
Please, Sign In to add comment