0

i have table with transactions data. I have to calculate median difference between transactions in days for every client. In table are 3 important columns:

**Transaction_id | client_id| transaction_date**

Transaction_id is primary. Have u any idea how i can handle it?

EDIT: One important info: I don't know how many transactions customers have.

Michal
  • 3
  • 3

1 Answers1

0

Hmmm . . . I think you want percentile_cont(). That would be:

select distinct client_id,
       percentile_cont(0.5) within group (order by day_diff) over (partition by client_id) as median_day_diff
from (select t.*,
             datediff(day,
                      lag(transaction_date) over (partition by client_id order by transaction_id,
                      transaction_date
                     ) as day_diff
      from t
     ) t
Gordon Linoff
  • 1,122,135
  • 50
  • 484
  • 624