Some commonly used SQL statements

First, the review group inquiry

groupQuery is grouped query, why grouping query? Because we want to press a statistical dimension. Let's look at a map:

Now my data as follows

For example, I want to know: 每天Java3y这个公众号的点击量是多少。according to our labor, the idea is very simple: the same number of days to find out the name and number for the public Java3y data, and then adding the amount per click, we come to the result.

 

step

We spend SQL might write:

select name,time,sum(pv) as pv  
from xxx_table 
where name = 'Java3y' group by name,time

1.1 group query misunderstanding that may exist

I remember one day, a group of friends asked a question on the group:

A problem inside the group

In fact, he's demand is simple: retrieve the highest record time after the data packet. But he was so dry:

  1. According to the first time order by

  2. For order byrecording the grouping

sample graph:

 

A group which FIG.

1.2 caused this misunderstanding of the possible causes

Some tools can support this wording:

select * from xxx_table group by name

The wording is not prohibited, and the results can be drawn, such as the results obtained are:

Java4y    20  7月15号
Java3y    30  7月15号

In fact, such an approach is unreasonable, want to know is: Use the group byfollowing group statistics, we can only follow behind the select group by a field, or aggregate functions.

     group by the rules

Because, we grouped the data query, data distribution, we do not care about .

Remember: the first group, after statistics (after the first data classification, then the same statistical data)

1.3 group most commonly used SQL query

Deduplication is a problem we often encounter, so to speak, for various reasons (whether on business or that are dirty data), and now I have two duplicate data (except ID, the rest of the fields are the same of):

 

Duplicate data

我这边只希望留下某一条记录作为查询结果就好了,我们可以写下以下的SQL:

select * from user where id in(
   select min(id) from user where name = 'Java3y' and pv = 20 and time='7-25' group by name,pv,time;
)

上面这条SQL是非常非常实用的,除了我说的去重以外,其实我们可以再”思考“一下:

  • 上面已经说了,使用group by分组统计之后,我们的select 后面只能跟着group by 的字段,或者是聚合函数。

  • 很多时候我们group by了以后,还想要查询结果中包含group by之外的字段(一般情况下,我们都不可能将group by 涵盖所有的字段),我们就可以上面那样,将查询后的结果作为子查询,放在外部查询的where 子句后,这样外部查询是可以select 出其他字段的。

(SQL写得比较少的朋友可能没什么感触啊,但我希望上面那种写法大家能够记住,以后一定会遇到类似的情况的)

二、回顾join查询

join查询不知道大家在刚学的时候是怎么理解的,反正我当初好像就挺迷迷糊糊的。我觉得join查询可以简单理解成这样:我想要的查询结果,一张表搞不掂,那我就join另一张表

比如说,现在我有两张的表:

第一张表

第二张表

现在我想知道在7月25号时:每个公众号的点击量、公众号名称、号主名称、公众号的创建日期

  • 显然,我们会发现一张表搞不掂啊,某些数据要依赖于另一张表才能把数据"完整"展示出来

     

    join其实就是一个合并的操作

那join其实就是把两张表合起来的一个操作:

两张表合并起来以后我们就会发现,这张“大表”就含有这两张表的所有字段啦,那我想要什么都有了!

值得注意的是:在join的时候,会产生笛卡尔积(至于什么是笛卡尔积我这里就不说了,反正我们要记住的是join表时一定要写关联条件去除笛卡尔积

另外,left joinright join也是我们经常用到,如果我们单纯写join关键字,那会被当成是inner join 。下面我简单解释一下:

  • 上面说了,在join的时候一定要写关联条件,如果是inner join的话,只有符合关联条件的数据才会存在最大表中

  • 如果是left join的话,即便关联条件不符合,左边表的数据一定会存在大表中

  • 如果是right join的话,即便关联条件不符合,右边表的数据一定会存在大表中

看下面的图:

 

join

此时我们的两张表关联的条件是“公众号” :如果是inner join,那么最后我们的表只有两条记录。如果是left join ,那么最后我们的表有三条数据。如果是right join,那么我们最后的表只有两条数据

三、回顾case when

SQL中的case when then else end用法其实跟我们程序语言中的if-else很是类似,在写SQL的时候也常常会用到。

我用得比较多的语法如下:

CASE WHEN sex = '1' THEN '男'
         WHEN sex = '2' THEN '女'
ELSE '其他' END  

在when后面可以跟多个表达式,比如说:

CASE WHEN sex = '1' and name ='Java3y' THEN '男'
         WHEN sex = '2' and name ='Java4y' THEN '女'
ELSE '其他' END  

如果要为case when表达式取别名,在end 关键字后边直接加就好了

更多用法详情参考:

  • https://www.cnblogs.com/prefect/p/5746624.html

四、一些常用的函数

4.1 hive和presto解析json

我这边会有这种情况:将json数据存到MySQL上。我去网上搜了一下以及问了同事,为什么要将json存到MySQL的字段上时,他们的答复都差不多:

  • 在MySQL存json数据,这样方便扩展啊。如果那些字段不需要用到索引,改动比较频繁,你又不想改动表的结构,那可以存json。

  • ps:在MySQL 5.7版本以后支持json类型

参考资料:

  • https://cloud.tencent.com/developer/article/1004449

  • https://www.zhihu.com/question/324674084/answer/685522547

我这边做报表一般来hive或presto上搞的,所以解析json的也是在那上面。

hive解析json函数:

get_json_object(param1,'$.param2')

-- 如果是数组
get_json_object(xjson,'$.[0].param2')

presto 对json的处理函数:

 -- 数组  (去除第index个json)
 json_array_get(xjson,index) 

 -- 单个jsoin对象
 json_extract(xjson,'$.param2')

参考资料:

  • https://www.cnblogs.com/drjava/p/10536922.html

4.2 时间函数

昨天/近7天/本月按照这种指标来查询也是非常常见的:

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1

在presto中使用时间格式,需要明确写出关键字timestamp,比如:

select supplier,count(id) 
from xxx_table 
where sendtime >= timestamp '2019-06-01' 

参考资料:

  • https://blog.csdn.net/cool_easy/article/details/50880949

4.3 其他常用的函数

这里我简单整理一下我最近用过函数:

length  --计算字符串长度
concat  --连接两个字符串
substring -- 截取字符串
count   -- 统计数量
max   -- 最大
min   -- 最小
sum   -- 合计
floor/ceil  --...数学函数

再来分享一下最近遇到的一个需求,现在有的数据如下:

【Java3y简单】快乐学习
【Java3y简单】快乐学习渣渣
【Java3y通俗易懂】简单学
【Java3y通俗易懂】简单学芭芭拉
【Java3y平易近人】无聊学
【Java3y初学者】枯燥学
【Java3y初学者】枯燥学呱呱
【Java3y大数据】欣慰学
【Java3y学习】巴拉巴拉学
【Java3y学习】巴拉巴拉学哈哈
【Java3y好】雨女无瓜学

现在我统计出【】括号里边出现的频次,比如说:Java3y通俗易懂出现的频次是多少。当时一直都没想到好的思路,都快要搜“SQL 正则表达式 快速入门”了,请教了一下同事,同事很快就写出来了:

select substring_index(left(title , INSTR(title , '】') -1 ) , '【',-1) 
FROM `xxx_table`

哇~,awesome

(完)

Guess you like

Origin www.cnblogs.com/xiaofengwang/p/11314536.html