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

SQL能力是作为数据产品经理必不可少的技能,当然,作为数据产品,我们对SQL的查询效率的要求可能不像开发那么高。而对于SQL的学习一般也是只需懂DQL查询语言就行,对于DCL、DDL、DML这些一般只是简单了解就可以。当然,SQL中会有一系类功能强大的函数,个人感觉,能够把常用的学会就能解决大部分的问题了,正所谓二八法则。对于日常业务中遇到的需要较为复杂的函数则可通过百度去查找。SQL的运用更多的是理解表之间的业务逻辑,才能够查询出满足业务需求的数据。这篇文章分享一下我在项目过程中常遇到的SQL函数,以后也会不断更新。

常规查询,进行表关联,常用的是left join和inner join,通过where和on进行条件限制,然后用order by和group by进行分组排序

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进行字段分层,在构建数据标签的时候常会用到,因为需要把不同范围的数据聚成一类

--收藏等级
--字段名: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取排名前几的数据,对于有些字段会对应多个值,而在构建数据指标时,一般只会取常用的值进行呈现,不会穷尽所有值

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()去除空值,并用正则匹配电话号码,一般涉及到电话号码相关,一般都会存在空值

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}$"

时间相关的函数,在涉及到用户登录信息相关的时候,常会与时间进行挂钩
–使用substr(…,…,…)取时间相关 1-4表示取到年 1-7表示取到月 1-10表示取到天
–使用round(…,…)进行取整,描述取到小数点后几位
–now()表示取当前时间下的时间
–year(now())表示取年
–取小时:hour()
–取天:day()
–取时间戳:unix_timestamp()
–months_sub(now(),1)取上一个月份
–months_sub(now(),6) 取近6个月
–year(years_sub(now(), 1))取上一年
–years_add(now(),1) 取未来一年内的时间
–datediff(now(),…)<time 取近time天
–时间戳转成标准时间:to_char(’’,‘yyyy-mm-dd’)

select  userid,count(distinct substr(sendTime,1,10)) '本年登录天数',
    count(UUID) '本年登录次数',
    round(count(UUID)/count(distinct substr(sendTime,1,10)),0) '日均登录次数',
    max(sendTime) '最近登陆时间',
    round(count(distinct substr(sendTime,1,10))/365,3) '登录天数占全年比重'
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,在构建数据指标和标签时,常会遇到很多空值,而为了更好的展现,一般会用默认值对空值进行填充

select materialid,ifnull(productid,'没有产品') from
pmc_kudu.designmaterial

聚合函数 求和sum() 计数count() 求平均avg()

#统计素材的拖拽信息,近30天拖拽情况#
select materialid,
sum(dragTimes) as darg_cnt30 --近30天拖拽次数
from 3D_point_kudu.materialDragInfo 
where datediff(now(),sendtime )<=30
group by materialid

运用start with … connect by prior…进行遍历查找,在日常业务存储中,对于多层关系常会用树状结构进行存储,则在查找的时候就需要进行遍历,比如:素材的id是具有多层层级结构的,要找到他的父节点,就需要进行遍历

--公共定制库的起始类别情况,起始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(…,…)函数从左取值,right(…,…)从右开始去规定范围内的字符数表

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

将毫秒的字符串相减 1)秒以上部分用时间戳转化,相减得到s单位 2)毫秒用right截取后,利用强制转化为整数

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)可用来可以作为表达式用,也可在存储过程中作为流程控制语句使用 如果 expr1
是TRUE,则返回值为expr2; 否则返回值为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定

select substr(createdate,1,7) as date_dl,
count(if(flag=0,true,null)) as xgt_cnt,--效果图数
count(if(flag=0 and width<=800 ,true,null)) bq_cnt,--标清
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)

最后,这篇文章是基于我毕业不到一年的认知所写的,有写得不对的地方欢迎和我交流。因为自己认识的做数据产品经理的朋友也比较少,不太清楚别人的数据产品经理是什么样子的。所以有想一起学习成长的朋友可以加个qq:624488342 ,一起交流沟通哈!

猜你喜欢

转载自blog.csdn.net/qq_41046286/article/details/105517992