博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql汇总以及gp改造优化
阅读量:6610 次
发布时间:2019-06-24

本文共 4348 字,大约阅读时间需要 14 分钟。

一、sql汇总

1.对xxxx@zzz.com 这种邮箱地址截取后缀

select right('zhsan@163.com',length('zhsan@163.com')-position('@' in 'zhsan@163.com'));

切割字符串,确定字符串长度,减去前半部分长度。

 

2.

:B表

 

:A表

 

:C表

用一条sql得到 c表的结果

SELECT id,sum(num) FROM(SELECT * from a UNION all SELECT * FROM b) as tmp GROUP BY id;

 

3.

user   name

1          a
1          a
1          b
2          c
2          c
2          d
想要分组以后得到
user   name   count
1          a          2
2          c          2

SELECT t1.* FROM (Select user,name,COUNT(1) AS count from T GROUP BY user,name)t1  INNER JOIN(SELECT t.user,MAX(t.count) AS count FROM (Select user,name,COUNT(1) AS count from T GROUP BY user,name)t  GROUP BY t.user)t2 ON t2.count = t1.count AND t2.user = t1.userORDER BY t1.user

 

 

应用到实际的查询中,username对应的mac地址中,groupby以后每组最多的mac地址的count

SELECT t1.* FROM (Select username,callingstationid,COUNT(1) AS count from radacctlog_2019_03 GROUP BY username,callingstationid)t1 INNER JOIN(SELECT t.username,MAX(t.count) AS count FROM  (Select username,callingstationid,COUNT(1) AS count from radacctlog_2019_03 GROUP BY username,callingstationid)t GROUP BY t.username)t2 ON t2.count = t1.count AND t2.username = t1.usernameORDER BY t1.username

 

 

SELECT t1.* FROM (Select username,callingstationid,COUNT(1) AS count from radacctlog_2019_03 GROUP BY username,callingstationid)t1 INNER JOIN(SELECT t.username,MAX(t.count) AS count FROM (Select username,callingstationid,COUNT(1) AS count from radacctlog_2019_03 GROUP BY username,callingstationid)t GROUP BY t.username)t2 ON t2.count = t1.count AND t2.username = t1.usernameORDER BY t1.username

 

4.

对某一天出现的数据行进行筛选

 

SELECT * FROM `radacctlog_2019_03` where acctstarttime <='2019-03-12 00:00:00'  and acctstoptime> '2019-03-13 00:00:00'  or acctstarttime BETWEEN'2019-03-12 00:00:00' and '2019-03-13 00:00:00' or  acctstoptime  BETWEEN'2019-03-12 00:00:00' and '2019-03-13 00:00:00';#指点时间段

 

对大于一天的数据进行筛选

SELECT * FROM `radacctlog_2019_03` WHERE acctstoptime-acctstarttime>86400#上行时间大于一天的

 

 

5.计算一张表中 挂科数大于二的同学的平均成绩

一条sql(逆向):

SELECT name,AVG(score),sum(score<60) as gks FROM result GROUP BY name HAVING gks>=2;

 

正常逻辑:

SELECT name,avg(score) from result WHERE name in  (SELECT a.name FROM(SELECT  name, score FROM result WHERE score<60)a GROUP BY a.name HAVING count(a.score)>=2) GROUP BY name

 

 

二、mysql迁移到gp改造

日期时间改造

SELECTto_date(to_char(a.acctstarttime, 'YYYY-MM-DD'),'YYYY-MM-DD') acctstarttime,to_date(to_char(a.acctstoptime, 'YYYY-MM-DD'),'YYYY-MM-DD') acctstoptime,EXTRACT(dow FROM a.acctstoptime),IF(    EXTRACT(HOUR FROM a.acctstarttime)=0.24,    EXTRACT(HOUR FROM a.acctstarttime)    )start_hourFROMradacct_time a

字符串拼接数据类型改造

SELECTeg.semester,ROUND( SUM(CAST(eg.credit as numeric) * cast(eg.point as numeric))/sum(cast(eg.credit as numeric)),2)as avg_credit_pointFROM dm_edu_grade_filter_r_semester egWHERE 1=1AND cast(eg.credit as numeric)>0AND SUBSTRING(eg.course_no,4,1)<>'9'AND eg.outid in(SELECT outid FROM dim_own_org_student_type_1WHERE        edu_status like (('%'||'1-')||'%'))        GROUP BY eg.semester

对sql count(distinct)优化改造

SELECT    b.opdt AS datetime,    SUM (b.opfare) AS sums,    COUNT (DISTINCT(b.outid)) AS numFROM    dm_m_rec_consume bWHERE    1 = 1GROUP BY    b.opdtORDER BY    b.opdt;
SELECT c.opdt as datetime,    sum(c.opfare) as sums,    count(*) as num     FROM    (SELECT b.opdt,b.opfare,b.outid     FROM dm_m_rec_consume b    GROUP BY b.opdt,b.opfare,b.outid)c    GROUP BY    c.opdt

 

其他一些gp中时间函数的使用

SELECT EXTRACT(HOUR FROM e.q)g FROM(SELECT to_date(to_char(a.admission_date,'YYYY-MM-DD'),'YYYY-MM-DD')qFROM  dim_own_org_student_type_1 a)e#获取小时SELECT EXTRACT(dow FROM e.q)g FROM(SELECT to_date(to_char(a.admission_date,'YYYY-MM-DD'),'YYYY-MM-DD')qFROM  dim_own_org_student_type_1 a)e#获取周几select EXTRACT(day from now()) #日期select EXTRACT(doy from now()) #一年的第几天select EXTRACT(month from now())#月份select EXTRACT(minute from now())#分钟select EXTRACT(week from now())#一年的第几周select EXTRACT(year from now())#年份select date_part('min','2015-01-15 17:05'::timestamp - '2015-01-13 15:01'::timestamp);#时间差SELECT to_date('05 Dec 2000','DD MonYYYY')#字符串转化为日期Select to_char(-125.8,'999D99S')#把数字转化为字符串SELECT to_char(INTERVAL'15h 2m 12s','HH24:MI:SS')#把时间间隔转化为时间SELECT to_char(125,'999')#把整数转化为字符串SELECT to_char(125.8::real,'999D9') #把实数/双精度数转化为字符串SELECT to_timestamp('05 Dec 2000','DD Mon YYYY') #把字符串转化为时间戳SELECT to_number('-12,454.8-','99G999D9S')#把字符串转化为数字 SELECT POSITION('a' in 'bcad' )#=>3获取某个字符串在一个字符串中的位置

 

转载于:https://www.cnblogs.com/languid/p/10985809.html

你可能感兴趣的文章
校园的早晨
查看>>
[学习]拆分成二维数组
查看>>
h5+ hbuilder ios提示语修改
查看>>
单例模式的5种实现方式,以及在多线程环境下5种创建单例模式的效率
查看>>
oracle取前几行|中间几行|后几行
查看>>
16.1 Tomcat介绍
查看>>
QuickBI助你成为分析师——数据源FAQ小结
查看>>
十周三次课
查看>>
S/4HANA服务订单Service Order的批量创建
查看>>
2008 AD 复制有防火墙要开什么端口
查看>>
IT服务管理中的知识库建设
查看>>
【Lucene】Lucene通过CustomScoreQuery实现自定义评分
查看>>
linux 内核网络,数据接收流程图
查看>>
我的友情链接
查看>>
在windows下与linux虚拟机进行文件共享
查看>>
php 图形用户界面GUI 开发
查看>>
正则表达式详解
查看>>
LeetCode问题5
查看>>
AIX系列------ISO挂载
查看>>
如何打开被管理员禁止的注册表编辑器
查看>>