日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

计算一列中某个值的个数

發(fā)布時(shí)間:2025/3/15 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计算一列中某个值的个数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

憶之獨(dú)秀?? ?https://blog.csdn.net/lavorange/article/details/25004181
這兩天在參加阿里大數(shù)據(jù)競(jìng)賽,進(jìn)入第二賽季要用到不少的SQL語(yǔ)句,現(xiàn)在才發(fā)現(xiàn)當(dāng)時(shí)的數(shù)據(jù)庫(kù)是白學(xué)了,今天就根據(jù)具體的需求來(lái)整理一下。可以把需求整理成為一道簡(jiǎn)單的題目,題目如下:

給定一個(gè)表t_alibaba_data:


user_id?? ?brand_id?? ?type?? ?visit_datetime
10944750?? ?13451?? ?0?? ?0604
10944750?? ?13451?? ?2?? ?0604
10944750?? ?13451?? ?2?? ?0723
10944750?? ?13451?? ?0?? ?0801
10944750?? ?13451?? ?0?? ?0515
10944750?? ?13451?? ?0?? ?0703
10944750?? ?13451?? ?0?? ?0629
10944750?? ?13451?? ?0?? ?0814
10944750?? ?21110?? ?0?? ?0422
10944750?? ?1131?? ?0?? ?0713
…?? ?…?? ?…?? ?…
其中:user_id代表用戶,brand_id代表品牌,type代表操作的類型(0-點(diǎn)擊,1-購(gòu)買,2-收藏,3-購(gòu)物車),visit_datetime即用戶對(duì)品牌行為的時(shí)間(0415-0515,0516-0615,0616-0715,0716-0815四個(gè)月)。假設(shè)一共有10000條交易記錄。
現(xiàn)在有這樣的需求即:我現(xiàn)在需要對(duì)用戶user_id對(duì)某一個(gè)品牌brand_id進(jìn)行評(píng)分grade(可以理解為用戶對(duì)品牌的喜愛程度),評(píng)分grade規(guī)則為用戶對(duì)品牌不同類型type=0,1,2,3的操作總數(shù),然后給這些總數(shù)乘以相應(yīng)的權(quán)值grade = w0*count(type=0) + w1*count(type=1) + w2*count(type=2)+w3*count(type=3)。當(dāng)然考慮到時(shí)間的因素,可以對(duì)靠后時(shí)間的點(diǎn)擊操作提高權(quán)重,相對(duì)距離較遠(yuǎn)的點(diǎn)擊操作降低權(quán)重,那么w0*count(type=0)可以改寫成

w0*( 1*count(type=0 and 0716<=visit_datatime<=0815) + 0.8*count(type=0 and 0616<=visit_datatime<=0715) +?

0.6*count(type=0 and 0516<=visit_datetime<=0615) ?+ 0.4*count(type=0 and 0415<=visit_datetime<=0515)).現(xiàn)在就是要?jiǎng)?chuàng)建一個(gè)表user_brand_grade<user_id,brand_id,grade>,按照grade降序排列以便找到前1000個(gè)<user_id,brand_id>對(duì)。

Step1:首先不考慮時(shí)間的因素,首先要找到用戶user_id對(duì)于某個(gè)品牌brand_id某種操作type=0/1/2/3的總次數(shù)。

SELECT user_id , brand_id , count(type=0) as type0 ,count(type=1) as type1 , count(type=2) as type2 ,count(type=3) as type3 FROM t_alibaba_data t where visit_datetime >= 415 && visit_datetime <= 715 group by user_id,brand_id;

結(jié)果:

當(dāng)我這么寫完之后我發(fā)現(xiàn)type0,type1,type2,type3的值都是一樣的。這非常不合理,才知道這么寫是有問(wèn)題的。
SQL語(yǔ)句count()的用法:http://www.w3school.com.cn/sql/sql_func_count.asp

可見count只能計(jì)算某一列的總數(shù)目,或者某一列符合一個(gè)條件的總數(shù)目,但不能計(jì)算某列符合多個(gè)條件的相應(yīng)數(shù)目。

Step2:那么修改count成為sum加上type的條件可以做出相應(yīng)的選擇。

SELECT user_id , brand_id , sum(type=0) as type0, sum(type=1) as type1, sum(type=2) as type2, sum(type=3) as type3 FROM t_alibaba_data t where visit_datetime >=515 && visit_datetime <=815 group by user_id ,brand_id;


結(jié)果:

SQL語(yǔ)句sum()的用法:http://www.w3school.com.cn/sql/sql_func_sum.asp

sum中是可以加表達(dá)式的,因此可以加上type的相應(yīng)值。

Step3:此時(shí)不禁要想,如果當(dāng)type=0的時(shí)候,要根據(jù)visit_datetime選擇不同的值怎么辦?在此要用到case進(jìn)行條件的選擇。那么就可以重寫Step2的SQL語(yǔ)句。

SELECT user_id , brand_id , sum(case when type=0 then 1 else 0 end) as type0, sum(case when type=1 then 1 else 0 end) as type1, sum(case when type=2 then 1 else 0 end) as type2, sum(case when type=3 then 1 else 0 end) as type3 FROM t_alibaba_data t group by user_id , brand_id ;


結(jié)果(應(yīng)該和上圖相同):


Step4:用case增加時(shí)間上的判斷:

select * ,(type0+type1+type2+type3) as grade from ( SELECT user_id , brand_id , sum(casewhen type=0 && visit_datetime >= 801 && visit_datetime<=815 then 10when type=0 && visit_datetime >= 716 && visit_datetime<=731 then 8when type=0 && visit_datetime >= 701 && visit_datetime<=715 then 6when type=0 && visit_datetime >= 616 && visit_datetime<=630 then 4when type=0 && visit_datetime >= 601 && visit_datetime<=615 then 2when type=0 && visit_datetime >= 516 && visit_datetime<=531 then 1else 0end) as type0, sum(case when type=1 then 1 else 0 end) as type1, sum(case when type=2 then 2 else 0 end) as type2, sum(case when type=3 then 3 else 0 end) as type3 FROM t_alibaba_data t group by user_id , brand_id order by type0 desc )a order by grade desc limit 1000;


然后得到結(jié)果:


那么就可以找到符合要求的<user_id,brand_id>對(duì)了,即最活躍的用戶品牌對(duì)。

總結(jié)

以上是生活随笔為你收集整理的计算一列中某个值的个数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。