Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

mysql - Aggregating data percentage wise based on date related criteria from a table

I have legacy tables which tracks flight and had to extract data. We have three tables named booking, airlines and flighttype. Note this is a dummy samples

booking :

id customer request_date airline flightType price currency
1 1 11-20-2020 10:23 1 1 120 Eur
2 1 11-21-2020 10:24 1 2 110 CHF
3 2 11-01-2020 11:25 2 2 120 Eur
4 1 15-01-2020 10:23 1 1 100 Eur
5 1 11-01-2020 11:23 1 2 60 Eur
6 1 12-01-2020 10:23 1 3 35 Eur

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I want to see the total revenue of customers who have made multiple booking within x days or customers who have made multiple booking with different set of currencies with x days

You can answer questions like this using window functions. For the first question, this looks like:

select count(distinct customer)
from (select b.*,
             lag(request_date) over (partition by customer order by request_date) as prev_request_date
      from booking b
     ) b
where request_date <= prev_request_date + interval <n> day;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...