简体   繁体   中英

SQL Server - CTE/Subqueries performance optimization with window functions

I'm getting the results I want with CTE, but is there a way to use window functions to get the same result, thus improving performance?

Desired output:

id   date                       Status
141  2015-03-01 00:00:00.000    free --> paid
141  2016-06-01 00:00:00.000    free --> paid
158  2015-08-01 00:00:00.000    free --> paid

CTE Code:

declare @table01 table (id varchar(3), startdate datetime, enddate datetime, status varchar(10))
insert into @table01 (id, startdate, enddate, status)
values
    ('141','2015-01-01','2015-03-01','free'),
    ('141','2015-03-01','2015-07-01','paid'),
    ('141','2015-07-01','2015-11-01','closed'),
    ('141','2015-11-01','2016-02-01','paid'),
    ('141','2016-02-01','2016-06-01','free'),
    ('141','2016-06-01','2016-10-01','paid'),
    ('141','2016-10-01','2016-12-01','free'),
    ('141','2016-12-01','2017-04-01','closed'),
    ('158','2015-03-01','2015-08-01','free'),
    ('158','2015-08-01','2015-11-01','paid');
------------------------------------------------------------------------------
with sub01 as (
    select id, enddate, status
    from @table01
    where status = 'free'
),
sub02 as (
    select id, startdate, status
    from @table01
    where status = 'paid'
)
select a.id, b.startdate as [date], (a.status + ' --> ' + b.status) as [Status]
from sub01 a
left join sub02 b on a.id = b.id and a.enddate = b.startdate
where a.enddate = b.startdate

If I understand correctly, you can use window functions:

select id, prev_date, 'free --> paid'
from (select t1.*,
             lag(enddate) over (partition by id order by startdate) as prev_date,
             lag(status) over (partition by id order by startdate) as prev_status
      from table1 t1
     ) t1
where status = 'paid' and prev_status = 'free';

Here is a db<>fiddle.

As you only interested in status = free and paid and the transition is on same date. It uses a case statement ( case when status = 'free' then enddate else startdate end ) to find the common date. Finally just group by id , date and with condition count(*) = 2

this should gives you better performance.

; with cte as
(
    select  id, 
            date    = case when status = 'free' then enddate else startdate end, 
            status
    from    @table01
    where   status in ( 'free', 'paid' )
)
select  id, date, status = 'free --> paid'
from    cte
group by id, date
having count(*) = 2
order by id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM