Anda dapat menggunakan trik untuk menghitung transaksi "tunai". Trik ini adalah perbedaan nomor baris dan sangat berguna:
select t.*
from (select t.*, count(*) over (partition by grp, customerid, transtype) as cnt
from (select t.*,
(row_number() over (partition by customerid order by date) -
row_number() over (partition by customerid, transtype order by date)
) as grp
from t
) t
where transtype = 'cash'
) t
where cnt >= 3;
Ini mengembalikan pelanggan dan tanggal mulai. Jika Anda ingin mengembalikan transaksi yang sebenarnya, Anda dapat menggunakan tingkat tambahan fungsi jendela:
select customerid, min(date) as start_date, sum(value) as sumvalue
from (select t.*,
(row_number() over (partition by customerid order by date) -
row_number() over (partition by customerid, transtype order by date)
) as grp
from t
) t
where transtype = 'cash'
group by grp, transtype, customerid
having count(*) >= 3;