1. 寫(xiě)sql查詢(xún)過(guò)去一個(gè)月付款用戶(hù)量(提示:用戶(hù)量需去重)最高的三天分別是哪幾天?
2. 寫(xiě)sql查詢(xún)昨天每個(gè)用戶(hù)最后付款的訂單ID及金額select date_format(pay_time,'%Y-%m-%d') days , count(distinct user_id) from table where pay_time>=date_sub(now(),interval 1 month) #過(guò)去一個(gè)月 group by date_format(pay_time,'%Y-%m-%d') order by count(distinct user_id) desc limit
3 思路:求最高的三天,肯定是先排序,后limit. 先求出每天的付款用戶(hù)量,既然每天,那肯定要按天分組了;按照題目要求過(guò)濾條件有:
1.過(guò)去一個(gè)月
2.付款用戶(hù)(即要排除未付款的用戶(hù)),另外求用戶(hù)量需要去重,題目中也有提示,因?yàn)榇嬖谕粋€(gè)用戶(hù)每天有多筆消費(fèi)記錄的情況;
返回排在前三的付款用戶(hù)量及對(duì)應(yīng)的時(shí)間(天) select a.user_id, a.order_amount from (select user_id, order_amount, row_number() over(partition by user_id order by pay_time desc) as rank from table where date_format(pay_time,"%Y-%m-%d")=date_sub(curdate(),interval 1 day) #昨天 ) as awhere rank=1