-2

I have table like this. From this i want the customer name who missed his last 3 appointments(Means his last 3 visit date is null).

+---------+------------------------+------------------------+
|Customer  |appointmentDate         |Vstdate                 |
+---------+------------------------+------------------------+
|a        |2017-01-01 00:00:00.000 |2017-01-01 10:42:33.793 |
|a        |2017-01-30 17:40:00.000 |NULL                    |
|a        |2017-01-30 17:40:00.000 |2017-01-30 19:52:39.000 |
|a        |2017-02-01 20:50:00.000 |2017-02-01 17:37:12.000 |
|a        |2013-02-14 12:20:59.407 |NULL                    |
|b        |2017-03-02 00:00:00.000 |2017-03-02 13:17:13.000 |
|b        |2017-03-25 12:30:00.000 |2017-03-25 14:30:00.000 |
|b        |2017-04-23 10:20:00.000 |NULL                    |
|b        |2017-05-27 12:30:00.000 |NULL                    |
|b        |2017-10-01 00:00:00.000 |NULL                    |
|c        |2017-03-02 00:00:00.000 |2017-03-02 13:17:13.000 |
|c        |2017-01-01 00:00:00.000 |2017-01-01 10:42:33.793 |
|c        |2017-01-30 17:40:00.000 |NULL                    |
|c        |2017-01-30 17:40:00.000 |2017-01-30 19:52:39.000 |
+---------+------------------------+------------------------+

For example from the above table only the customer b has missed his last 3 appointments(Means his last 3 visit date is null). I want only his name to be retried from the table.

DevS
  • 83
  • 1
  • 7

2 Answers2

1

Use top 3:

select distinct customer from tmp c where (
  select max(vstdate) from (
    select top 3 vstdate from tmp e where e.customer = c.customer
    order by appointmentDate desc
  ) x
) is null;

Result:

customer  
----------
b         
The Impaler
  • 45,731
  • 9
  • 39
  • 76
0

Use a window function:

declare @tmp table(Customer varchar(1), appointmentDate datetime2, Vstdate datetime2)
insert into @tmp values
 ('a', '2017-01-01 00:00:00.000', '2017-01-01 10:42:33.793')
,('a', '2017-01-30 17:40:00.000', NULL                     )
,('a', '2017-01-30 17:40:00.000', '2017-01-30 19:52:39.000')
,('a', '2017-02-01 20:50:00.000', '2017-02-01 17:37:12.000')
,('a', '2013-02-14 12:20:59.407', NULL                     )
,('b', '2017-03-02 00:00:00.000', '2017-03-02 13:17:13.000')
,('b', '2017-03-25 12:30:00.000', '2017-03-25 14:30:00.000')
,('b', '2017-04-23 10:20:00.000', NULL                     )
,('b', '2017-05-27 12:30:00.000', NULL                     )
,('b', '2017-10-01 00:00:00.000', NULL                     )
,('c', '2017-03-02 00:00:00.000', '2017-03-02 13:17:13.000')
,('c', '2017-01-01 00:00:00.000', '2017-01-01 10:42:33.793')
,('c', '2017-01-30 17:40:00.000', NULL                     )
,('c', '2017-01-30 17:40:00.000', '2017-01-30 19:52:39.000')

select t.Customer 
from (
    select *, row_number() over (partition by Customer order by appointmentDate desc) as ord
    from @tmp
) t
where t.ord <= 3 
group by t.Customer
having max(t.Vstdate) is null

Result:

enter image description here

Andrea
  • 11,801
  • 17
  • 65
  • 72