I have several usage types. One is to prepare another is actual usage.

*Need to check if prepaid type start and end date in the previous period = (equal) start and end dates in next period for usage*

At the end of the period, I have prepared usage, according to the logic next period usage must match prepaid from the previous period. But some times I have cases where these two types don't match.
For example

```
  1  (128193    ,'2020-05-01',  '2020-05-31','prepaid'),
  2  (134145    ,'2020-06-01',  '2020-06-30','prepaid'),
  3  (134145    ,'2020-05-01',  '2020-05-30','usage'),

```

Example record number 3 should match record number 1 as it pre paid for that period, in this situation they **usage** start and end date in period 134145 do not mach start and end date of **prepaid** in previous period 128193.

I need to find those cases.

I have created a script

```sql
DECLARE @t TABLE ( id INT, startdate DATE, enddate DATE , u_type VARCHAR(255))

INSERT  INTO @t
VALUES  ( 128193    ,'2020-05-01',  '2020-05-31','prepaid'),
         (134145    ,'2020-06-01',  '2020-06-30','prepaid'),
         (134145    ,'2020-05-01',  '2020-05-30','usage'),
         (141461    ,'2020-07-01',  '2020-07-31','prepaid'),
         (141461    ,'2020-06-01',  '2020-06-30','usage');
WITH    cte
          AS ( SELECT   ROW_NUMBER() OVER ( ORDER BY id ) AS r ,
                        id ,
                        startdate ,
                        enddate,
                        u_type
               FROM     @t
             )
    SELECT  c2.id ,
            c1.id, 
            c1.startdate,
            c1.enddate,
            c2.startdate,
            c2.enddate,
            c1.u_type,
            c2.u_type
    FROM    cte c1
            JOIN cte c2 ON c2.r + 1 = c1.r AND  c2.u_type <> c1.u_type
    WHERE   DATEADD(d, 1, c2.enddate) <> c1.startdate

```

but it returns me the incorrect results.

It is one time run so I don't care about performance speed can use whatever will require as #tables CTEs, any ideas on how to fix it?

The prepaid record always goes for the future period.