DEV Community

Judy
Judy

Posted on

How to Convert Dates in One Group into An Interval

We have a database table TBLTEST. Below is part of its data:

Image description
The data is ordered by date. We are trying to group rows by the first five columns, convert dates in each group into an interval, and record the ending date of the last record as the infinite date 99991230. Below is the desired result:

Image description
Oracle SQL:

SELECT
     code1,
     code2,
     code3,
     rate,
     value,
     min(MONTH) start_dt,
     CASE
          WHEN ROW_NUMBER() OVER(PARTITION BY code1, code2, code3 ORDER BYmax(MONTH) DESC) = 1 THEN 99991230
          ELSE max(MONTH)
     END end_dt
FROM
     (
     SELECT
          t.*,
          ROW_NUMBER() OVER(PARTITION BY code1, code2, code3 ORDER BY MONTH) rn1,
          ROW_NUMBER() OVER(PARTITION BY code1, code2, code3, rate, value ORDERBY MONTH) rn2
     FROM
          TBLTEST t
) t
GROUP BY
     code1,
     code2,
     code3,
     rate,
     value,
     rn1 - rn2
ORDER BY
     start_dt
Enter fullscreen mode Exit fullscreen mode

It is rather simple to perform the task in the natural way of thinking. We compare neighboring values between rows on the first five columns, and put the current one and the previous row in the same group when values are same, or create a new group if they are different until the last record is compared. As SQL set is unordered, we need to first invent two columns of indexes manually in an extremely complicated way and then perform grouping according to the relationship between the two columns of indexes. You need to be really smart to come up with the solution.

Yet it is easy to write the code using the open-source esProc SPL:

Image description
SPL supports ordered sets directly, making it easy to perform grouping when a neighboring value is different.

Top comments (0)