Condense Time Periods with SQL

前端 未结 4 1555
春和景丽
春和景丽 2021-02-20 17:41

I have a large data set which for the purpose of this question has 3 fields:

  • Group Identifier
  • From Date
  • To Date

On any given row t

4条回答
  •  情歌与酒
    2021-02-20 18:19

    A Geometric Approach

    Here and elsewhere I've noticed that date packing questions don't provide a geometric approach to this problem. After all, any range, date-ranges included, can be interpreted as a line. So why not convert them to a sql geometry type and utilize geometry::UnionAggregate to merge the ranges. So I gave a stab at it with your post.

    Code Description

    In 'numbers':

    • I build a table representing a sequence
    • Swap it out with your favorite way to make a numbers table.
    • For a union operation, you won't ever need more rows than in your original table, so I just use it as the base to build it.

    In 'mergeLines':

    • I convert the dates to floats and use those floats to create geometrical points.
    • In this problem, we're working in 'integer space,' meaning there are no time considerations, and so an begin date in one range that is one day apart from an end date in another should be merged with that other. In order to make that merge happen, we need to convert to 'real space.', so we add 1 to the tail of all ranges (we undo this later).
    • I then connect these points via STUnion and STEnvelope.
    • Finally, I merge all these lines via UnionAggregate. The resulting 'lines' geometry object might contain multiple lines, but if they overlap, they turn into one line.

    In the outer query:

    • I use the numbers CTE to extract the individual lines inside 'lines'.
    • I envelope the lines which here ensures that the lines are stored only as its two endpoints.
    • I read the endpoint x values and convert them back to their time representations, ensuring to put them back into 'integer space'.

    The Code

    with
    
        numbers as (
    
            select  row_number() over (order by (select null)) i 
            from    @spans -- Where I put your data
    
        ),
    
        mergeLines as (
    
            select      groupId,
                        lines = geometry::UnionAggregate(line)
            from        @spans
            cross apply (select 
                            startP = geometry::Point(convert(float,fromDate), 0, 0),
                            stopP = geometry::Point(convert(float,toDate) + 1, 0, 0)
                        ) pointify
            cross apply (select line = startP.STUnion(stopP).STEnvelope()) lineify
            group by    groupId 
    
        )
    
        select      groupId, fromDate, toDate 
        from        mergeLines ml
        join        numbers n on n.i between 1 and ml.lines.STNumGeometries()
        cross apply (select line = ml.lines.STGeometryN(i).STEnvelope()) l
        cross apply (select 
                        fromDate = convert(datetime, l.line.STPointN(1).STX),
                        toDate = convert(datetime, l.line.STPointN(3).STX) - 1
                    ) unprepare
        order by    groupId, fromDate;
    

提交回复
热议问题