Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come identificare il primo divario in più intervalli di date di inizio e fine per ogni membro distinto in T-SQL

Prova questo:http://www.sqlfiddle.com/#!3/c3365/ 20

with s as
(
  select *, row_number() over(partition by membercode order by startdate) rn
  from tbl
)
,gaps as
(
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
  ,datediff(d, a.enddate, b.startdate) as gap
from s a
join s b on b.membercode = a.membercode and b.rn = a.rn + 1
)
select membercode 
from gaps
group by membercode
having sum(case when gap <= 1 then 1 end) = count(*);

Vedi la progressione della query qui:http://www.sqlfiddle.com/#!3/ c3365/20

Come funziona, confronta la data di fine corrente con la data di inizio successiva e controlla la differenza di date:

with s as
(
  select *, row_number() over(partition by membercode order by startdate) rn
  from tbl
)
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
  ,datediff(d, a.enddate, b.startdate) as gap
from s a
join s b on b.membercode = a.membercode and b.rn = a.rn + 1;

Uscita:

| MEMBERCODE |  STARTDATE |    ENDDATE | NEXTSTARTDATE | GAP |
--------------------------------------------------------------
|          1 | 2010-01-15 | 2010-01-20 |    2010-01-19 |  -1 |
|          1 | 2010-01-19 | 2010-01-22 |    2010-01-20 |  -2 |
|          1 | 2010-01-20 | 2010-01-25 |    2010-01-26 |   1 |
|          2 | 2010-01-20 | 2010-01-25 |    2010-01-30 |   5 |
|          2 | 2010-01-30 | 2010-02-05 |    2010-02-04 |  -1 |

Quindi controlla se un membro ha lo stesso numero di reclami senza interruzioni rispetto al totale dei reclami:

with s as
(
  select *, row_number() over(partition by membercode order by startdate) rn
  from tbl
)
,gaps as
(
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
  ,datediff(d, a.enddate, b.startdate) as gap
from s a
join s b on b.membercode = a.membercode and b.rn = a.rn + 1
)
select membercode, count(*) as count, sum(case when gap <= 1 then 1 end) as gapless_count
from gaps
group by membercode;

Uscita:

| MEMBERCODE | COUNT | GAPLESS_COUNT |
--------------------------------------
|          1 |     3 |             3 |
|          2 |     2 |             1 |

Infine, filtrali, membri senza lacune nelle loro affermazioni:

with s as
(
  select *, row_number() over(partition by membercode order by startdate) rn
  from tbl
)
,gaps as
(
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
  ,datediff(d, a.enddate, b.startdate) as gap
from s a
join s b on b.membercode = a.membercode and b.rn = a.rn + 1
)
select membercode 
from gaps
group by membercode
having sum(case when gap <= 1 then 1 end) = count(*);

Uscita:

| MEMBERCODE |
--------------
|          1 |

Tieni presente che non è necessario eseguire COUNT(*) > 1 per rilevare i membri con 2 o più reclami. Invece di usare LEFT JOIN , utilizziamo JOIN , questo eliminerà automaticamente i membri che devono ancora avere una seconda richiesta. Ecco la versione (più lunga) se scegli di utilizzare LEFT JOIN invece (stesso output di cui sopra):

with s as
(
select *, row_number() over(partition by membercode order by startdate) rn
from tbl
)
,gaps as
(
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
,datediff(d, a.enddate, b.startdate) as gap
from s a
left join s b on b.membercode = a.membercode and b.rn = a.rn + 1
)
select membercode 
from gaps
group by membercode
having sum(case when gap <= 1 then 1 end) = count(gap)
and count(*) > 1; -- members who have two ore more claims only

Ecco come visualizzare i dati della query precedente prima del filtraggio:

with s as
(
  select *, row_number() over(partition by membercode order by startdate) rn
  from tbl
)
,gaps as
(
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
  ,datediff(d, a.enddate, b.startdate) as gap
from s a
left join s b on b.membercode = a.membercode and b.rn = a.rn + 1
)
select * from gaps;

Uscita:

| MEMBERCODE |  STARTDATE |    ENDDATE | NEXTSTARTDATE |    GAP |
-----------------------------------------------------------------
|          1 | 2010-01-15 | 2010-01-20 |    2010-01-19 |     -1 |
|          1 | 2010-01-19 | 2010-01-22 |    2010-01-20 |     -2 |
|          1 | 2010-01-20 | 2010-01-25 |    2010-01-26 |      1 |
|          1 | 2010-01-26 | 2010-01-30 |        (null) | (null) |
|          2 | 2010-01-20 | 2010-01-25 |    2010-01-30 |      5 |
|          2 | 2010-01-30 | 2010-02-05 |    2010-02-04 |     -1 |
|          2 | 2010-02-04 | 2010-02-15 |        (null) | (null) |
|          3 | 2010-02-15 | 2010-03-02 |        (null) | (null) |

MODIFICA sul chiarimento dei requisiti:

Sul tuo chiarimento, volevi includere anche i membri che devono ancora avere una seconda richiesta, invece fai questo:http://sqlfiddle.com/#!3/c3365/22

with s as
(
select *, row_number() over(partition by membercode order by startdate) rn
from tbl
)
,gaps as
(
select a.membercode, a.startdate, a.enddate, b.startdate as nextstartdate
,datediff(d, a.enddate, b.startdate) as gap
from s a
left join s b on b.membercode = a.membercode and b.rn = a.rn + 1
)
select membercode 
from gaps
group by membercode
having sum(case when gap <= 1 then 1 end) = count(gap)
-- members who have yet to have a second claim are valid too
or count(nextstartdate) = 0; 

Uscita:

| MEMBERCODE |
--------------
|          1 |
|          3 |

La tecnica consiste nel contare il nextstartdate del membro , se non hanno una data di inizio successiva (ad esempio count(nextstartdate) = 0 ) allora sono solo rivendicazioni singole e valide, quindi allega semplicemente questo OR condizione:

or count(nextstartdate) = 0; 

In realtà, anche la condizione seguente sarà sufficiente, volevo però rendere la query più auto-documentante, quindi consiglio di contare sulla data di inizio successiva del membro. Ecco una condizione alternativa per il conteggio dei membri che devono ancora avere una seconda richiesta:

or count(*) = 1;

A proposito, dobbiamo anche cambiare il confronto da questo:

sum(case when gap <= 1 then 1 end) = count(*)

a questo (dato che stiamo usando LEFT JOIN ora):

sum(case when gap <= 1 then 1 end) = count(gap)