Я думаю, что вы можете сделать это, используя простое условие where. Предполагая, что поля даты хранятся в формате даты, вычисляя за последние 10 дней (Это на SQL сервере):
select Customer,count(*)
from table
where Complaint_date<transaction_date
and Complaint_date between GETDATE()-10 and GETDATE()
Group by Customer
Надеюсь, это поможет.
Редактировать: Вы можете попробуйте выполнить самообъединение здесь:
;with tableA as
(select 'A' as customer,1 as Transaction_date,2 as Complaint_date union all
select 'A',1,3 union all
select 'A',2,3 union all
select 'B',1,2 union all
select 'B',1,2 union all
select 'B',2,3 union all
select 'C',2,3)
select a.Customer,a.transaction_date,count(b.Complaint_date) as cnt
from tableA a
left join tableA b on a.customer=b.customer
and a.transaction_date>=b.Complaint_date
Group by a.Customer,a.transaction_date
Это приведет к следующему выводу:
Customer Transaction_Date Cnt
A 1 0
A 2 1
B 1 0
B 2 2
C 2 0
Если вы хотите получить такой же вывод, как упомянуто в вашем ответе, вы можете присоединиться к этому выводу обратно к Исходный стол:
;with tableA as
(select 'A' as customer,1 as Transaction_date,2 as Complaint_date union all
select 'A',1,3 union all
select 'A',2,3 union all
select 'B',1,2 union all
select 'B',1,2 union all
select 'B',2,3 union all
select 'C',2,3)
select x.*,y.cnt
from tableA x
inner join
(select a.Customer,a.transaction_date,count(b.Complaint_date) as cnt
from tableA a
left join tableA b on a.customer=b.customer
and a.transaction_date>=b.Complaint_date
Group by a.Customer,a.transaction_date) y
ON x.customer=y.customer and x.transaction_date=y.Transaction_date