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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

数据产品-数据指标标签常用sql函数

發(fā)布時間:2025/3/17 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据产品-数据指标标签常用sql函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SQL能力是作為數(shù)據(jù)產(chǎn)品經(jīng)理必不可少的技能,當(dāng)然,作為數(shù)據(jù)產(chǎn)品,我們對SQL的查詢效率的要求可能不像開發(fā)那么高。而對于SQL的學(xué)習(xí)一般也是只需懂DQL查詢語言就行,對于DCL、DDL、DML這些一般只是簡單了解就可以。當(dāng)然,SQL中會有一系類功能強(qiáng)大的函數(shù),個人感覺,能夠把常用的學(xué)會就能解決大部分的問題了,正所謂二八法則。對于日常業(yè)務(wù)中遇到的需要較為復(fù)雜的函數(shù)則可通過百度去查找。SQL的運用更多的是理解表之間的業(yè)務(wù)邏輯,才能夠查詢出滿足業(yè)務(wù)需求的數(shù)據(jù)。這篇文章分享一下我在項目過程中常遇到的SQL函數(shù),以后也會不斷更新。

常規(guī)查詢,進(jìn)行表關(guān)聯(lián),常用的是left join和inner join,通過where和on進(jìn)行條件限制,然后用order by和group by進(jìn)行分組排序

select a.materialid,sum(a.dragtimes) as use_yx_cnt30 from (select materialid,dragtimes,schemeid from 3D_point_kudu.materialInfo where datediff(now(),sendtime )<=30)a left join (select id,scheme_name from ugc_scheme_kudu.home_scheme)b on a.schemeid=b.id group by a.materialid order by use_yx_cnt30 desc

使用case when…then…else…end進(jìn)行字段分層,在構(gòu)建數(shù)據(jù)標(biāo)簽的時候常會用到,因為需要把不同范圍的數(shù)據(jù)聚成一類

--收藏等級 --字段名:save_level select a.materialid,b.collectcount, case when b.collectcount>=2000 then '五顆星'when b.collectcount>=1000 and b.collectcount<2000 then '四顆星'when b.collectcount>=500 and b.collectcount<1000 then '三顆星'when b.collectcount>=100 and b.collectcount<500 then '兩顆星'when b.collectcount<100 then '一顆星'end as save_level from (select materialid from pmc_kudu.designmaterial where organid='C00000022' and is3D=1 and isdelete=0)a inner join (select materialid,collectcount from pmc_kudu.modelcollectext where collectcount>0)b on a.materialid=b.materialid

使用row_number() over(partition by…order by…) as row_id…where row_id<=n取排名前幾的數(shù)據(jù),對于有些字段會對應(yīng)多個值,而在構(gòu)建數(shù)據(jù)指標(biāo)時,一般只會取常用的值進(jìn)行呈現(xiàn),不會窮盡所有值

select * from( select t.mobile, t.userid, row_number() over (partition by t.userid order by t.REGDATE desc) as row_id from (--獲取注冊時登記的號碼 select e.mobile,u.userid,u.REGDATE from syscore_kudu.users u inner join syscore_kudu.employee e on u.userid=e.userid where trim(e.mobile) REGEXP "^[1]([3-9])[0-9]{9}$" ) t )t where t.row_id=1

使用trim()去除空值,并用正則匹配電話號碼,一般涉及到電話號碼相關(guān),一般都會存在空值

select e.mobile,u.userid,u.REGDATE from syscore_kudu.users u inner join syscore_kudu.employee e on u.userid=e.userid where trim(e.mobile) REGEXP "^[1]([3-9])[0-9]{9}$"

時間相關(guān)的函數(shù),在涉及到用戶登錄信息相關(guān)的時候,常會與時間進(jìn)行掛鉤
–使用substr(…,…,…)取時間相關(guān) 1-4表示取到年 1-7表示取到月 1-10表示取到天
–使用round(…,…)進(jìn)行取整,描述取到小數(shù)點后幾位
–now()表示取當(dāng)前時間下的時間
–year(now())表示取年
–取小時:hour()
–取天:day()
–取時間戳:unix_timestamp()
–months_sub(now(),1)取上一個月份
–months_sub(now(),6) 取近6個月
–year(years_sub(now(), 1))取上一年
–years_add(now(),1) 取未來一年內(nèi)的時間
–datediff(now(),…)<time 取近time天
–時間戳轉(zhuǎn)成標(biāo)準(zhǔn)時間:to_char(’’,‘yyyy-mm-dd’)

select userid,count(distinct substr(sendTime,1,10)) '本年登錄天數(shù)',count(UUID) '本年登錄次數(shù)',round(count(UUID)/count(distinct substr(sendTime,1,10)),0) '日均登錄次數(shù)',max(sendTime) '最近登陸時間',round(count(distinct substr(sendTime,1,10))/365,3) '登錄天數(shù)占全年比重' from 3d_point_kudu.userinfo where substr(sendTime,1,4) = cast(year(now()) as string) and length(sendTime)=19 group by userid;

IFNULL(expr1,expr2),如果expr1不是NULL,IFNULL()返回expr1,否則它返回expr2,在構(gòu)建數(shù)據(jù)指標(biāo)和標(biāo)簽時,常會遇到很多空值,而為了更好的展現(xiàn),一般會用默認(rèn)值對空值進(jìn)行填充

select materialid,ifnull(productid,'沒有產(chǎn)品') from pmc_kudu.designmaterial

聚合函數(shù) 求和sum() 計數(shù)count() 求平均avg()

#統(tǒng)計素材的拖拽信息,近30天拖拽情況# select materialid, sum(dragTimes) as darg_cnt30 --近30天拖拽次數(shù) from 3D_point_kudu.materialDragInfo where datediff(now(),sendtime )<=30 group by materialid

運用start with … connect by prior…進(jìn)行遍歷查找,在日常業(yè)務(wù)存儲中,對于多層關(guān)系常會用樹狀結(jié)構(gòu)進(jìn)行存儲,則在查找的時候就需要進(jìn)行遍歷,比如:素材的id是具有多層層級結(jié)構(gòu)的,要找到他的父節(jié)點,就需要進(jìn)行遍歷

--公共定制庫的起始類別情況,起始categoryid='2123602' SELECT categoryid,parentid,CATEGORYNAME, sys_connect_by_path(CATEGORYNAME,'->') NAME FROM pmc.categorynew t START WITH t.categoryid = '2123602' CONNECT BY t.parentid = prior t.categoryid and t.isdelete=0

使用left(…,…)函數(shù)從左取值,right(…,…)從右開始去規(guī)定范圍內(nèi)的字符數(shù)表

select materialid,left(materialname,7),right(materialname,7) from pmc_kudu.designmaterial limit 10

將毫秒的字符串相減 1)秒以上部分用時間戳轉(zhuǎn)化,相減得到s單位 2)毫秒用right截取后,利用強(qiáng)制轉(zhuǎn)化為整數(shù)

select starttime,endtime, (unix_timestamp(endtime)-unix_timestamp(starttime))*1000+ cast(right(endtime,length(endtime)-20) as int)-cast(right(starttime,length(starttime)-20) as int) ms from performance

使用if(expr1,expr2,expr3)可用來可以作為表達(dá)式用,也可在存儲過程中作為流程控制語句使用 如果 expr1 是TRUE,則返回值為expr2; 否則返回值為 expr3。IF() 的返回值為數(shù)字值或字符串值,具體情況視其所在語境而定

select substr(createdate,1,7) as date_dl, count(if(flag=0,true,null)) as xgt_cnt,--效果圖數(shù) count(if(flag=0 and width<=800 ,true,null)) bq_cnt,--標(biāo)清 count(if(flag=0 and width>800 and width<=1280,true,null)) gq_cnt,--高清 from (select flag,createdate,width from pmc_kudu.queueok)b group by substr(createdate,1,7)

最后,這篇文章是基于我畢業(yè)不到一年的認(rèn)知所寫的,有寫得不對的地方歡迎和我交流。因為自己認(rèn)識的做數(shù)據(jù)產(chǎn)品經(jīng)理的朋友也比較少,不太清楚別人的數(shù)據(jù)產(chǎn)品經(jīng)理是什么樣子的。所以有想一起學(xué)習(xí)成長的朋友可以加個qq:624488342 ,一起交流溝通哈!

總結(jié)

以上是生活随笔為你收集整理的数据产品-数据指标标签常用sql函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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