之前有人問我,如何挑出一個月的最大值及其特征,比如有三列,分別是user_id,item_id,time,其中time是停留時長,這個問題就是找出這個用戶,他這一行是什么,我當時就懵逼了,我說我直接全部拉下來這個月的資料,然后py操作取最大值即可,,,game over
For Recommendation in Deep learning QQ Group 277356808
For deep learning QQ Second Group 629530787
I'm here waiting for you
下面分步進行,逐漸加深
1-按照time來排序獲取一定時間內的點擊曝光日志,從小到大的排序
既然是排序,那么肯定是用ORDER BY,如下是簡單的按照單列(time)排序
SELECT user_id,item_id,time
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
ORDER BY time
這里我要驗證一下,是否是按照str(time)的形式(有沒有str這個函式我不知道哈,這是py的表達方式)排序,還是按照float(time)的形式(這種是有的)排序
【where concat 用法參考此博文】
其結果如下:默認按照字串順序來排的,而不是浮點數大小,經查是存在10~100之間的數
2038 e0eac9 8C7c 10.995
2039 b83a31 8C5I 10.997
2040 c2313e 83XL 10.997
2041 d3f66a 8CQ7 100.0
2042 1c79d5 7sxn 100.0
2043 b7c62f 8CLT 100.0
2044 37833b 8CPh 100.0
改成float經驗證是正確的,如下示例
#ORDER BY float(time)
12265 de27 8CLV 19.996
12266 73bc 8CQV 20.0
12267 0461 8CQQ 20.0
2-按照user_id,time順序來排序,這個可以得到每個用戶的time排序
這里直接按照time浮點排序哈,不加float還是默認按照字串排序,這是不符合我的本意的
SELECT user_id,item_id,time
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
ORDER BY user_id,float(time)
示例如下:那么如何獲取每個用戶的最長time的那一行呢?這才是問題
17b2 T2Uh 2.256
17b2 uLMX 31.76
9695 t7zD 1.206
9695 85w8 1.255
9695 8CCg 117.253
f270 8C3i 6.197
f270 8C10 11.326
f270 8Btv 132.45
f270 8CLe 343.339
為了防止按照字串排序,我都加上float,這樣就一定按照數字大小排序了
同事大佬只說了row_number,然而我還是沒有得到每個用戶的最長time的那行
SELECT user_id,item_id,time,row_number() over (partition by user_id order by float(time))
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
fe87 8CN1 184.385 3
fe87 YQqA 311.246 4
fe87 8BqA 311.246 5
fe87 8wfh 713.201 6
fe8f 8BWi 25.878 1
fe8f 8COA 169.28 2
fe8f 8ARA 191.654 3
這個無非是每個用戶觀看的時長排序,增加了一列編號而已,與上面的order by兩列并無區別,
給上面的time按照倒序排列,只是加個desc,如下:這樣最大的就排第一了
SELECT user_id,item_id,time,row_number() over (partition by user_id order by float(time) desc)
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
05b0 8CM2 373.614 1
05b0 8AR1 358.413 2
05b0 8ATg 358.392 3
05b0 8AR1 358.359 4
,,
0558 8CF9 227.706 1
0558 877A 55.612 2
0558 85iI 48.616 3
那么只需再取其中列為1的即可了,然而加上select就錯了,woc,發生了什么鬼?mmp
SELECT user_id,item_id,time
From
( SELECT user_id,item_id,time,row_number() over (partition by user_id order by float(time) desc)
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
) #這里需要加新表名,隨意,比如new_tab
where rn==1
ParseException line 1:26 cannot recognize input near '(' 'select' 'user_id' in joinSource
同事大佬一下子指出來了錯誤,中間那一堆是個新表,需要命名一個表名,然后結果如下:
fed0 8zOY 17.159 1
feee 8Cvc 66.48599999999999 1
fe29 8Vh5 708.173 1
ffee 8CWo 30.55 1
ff65 FQEO 39.327 1
ff8c GZxa 47.989 1
ffa9 M4aY 17.056 1
ffa9 FQEO 19.407 1
3-該段時間內的最大值,
這個直接倒排取第一個也可以,方法一:去掉上面的partition
SELECT user_id,item_id,time
From
( SELECT user_id,item_id,time,row_number() over (order by float(time) desc)
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
) new_tab
where rn==1
edaf hu4W 2682.734 1
方法二:加個limit 1,哈哈這個也行吧,思維打開,關注我,縱享絲滑!
SELECT user_id,item_id,time
FROM ClickLogTable
WHERE concat(datetime, dayhour) between 2022010101 and 2022010102
and user_id is not NULL and time is not NULL
ORDER BY float(time) desc limit 1
edaf hu4W 2682.734
拜拜
愿我們終有重逢之時,
而你還記得我們曾經討論的話題,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/436408.html
標籤:其他
上一篇:記一次RocketMQConsumer 服務關閉出現InterruptException例外
下一篇:幾分鐘明白Flink水位線
