Aggregate adjacent only records with T-SQL

前端 未结 4 1834
孤街浪徒
孤街浪徒 2020-12-17 04:09

I have (simplified for the example) a table with the following data

Row Start       Finish       ID  Amount
--- ---------   ----------   --  ------
  1 2008-         


        
4条回答
  •  不思量自难忘°
    2020-12-17 04:57

    I suspect that it may require iteration of some sort but I don't want to go down that path.

    I think that's the route you'll have to take, use a cursor to populate a table variable. If you have a large number of records you could use a permanent table to store the results then when you need to retrieve the data you could process only the new data.

    I would add a bit field with a default of 0 to the source table to keep track of which records have been processed. Assuming no one is using select * on the table, adding a column with a default value won't affect the rest of your application.

    Add a comment to this post if you want help coding the solution.

提交回复
热议问题