SQL 十分位
-- 十分位,这个算法不是很准确
select
family_agreement_cnt -- 字段
,dt -- 分区
,rn -- 排序
,cnt -- 总行数
,percent2 -- 分位值
,rk
,row_num
from (
select
t1.family_agreement_cnt -- 字段
,t1.dt -- 分区
,t1.rn -- 排序
,t1.cnt -- 总行数
,ceil(t1.rn / t1.cnt * 100) as percent2 -- 分位值
,row_number() over(partition by ceil(t1.rn / t1.cnt * 100) order by rn desc) as rk
,row_number() over(order by rn) as row_num
from (
select
family_agreement_cnt
,dt
,row_number() over(partition by dt order by cast(family_agreement_cnt as double)) as rn
,count(1) over(partition by dt) as cnt
from table_name
where dt=''
) t1
where t1.rn = 1 or t1.rn % cast(t1.cnt/10 as int) = 0 or t1.rn = t1.cnt
order by t1.dt,t1.rn
) t2
where t2.rk = 1
; -- 方差
select
stddev(num) as std
from (
select 1 as num union all
select 2 as num union all
select 3 as num union all
select 4 as num union all
select 5 as num union all
select 6 as num union all
select 7 as num union all
select 8 as num union all
select 9 as num union all
select 10 as num union all
select 11 as num union all
select 12 as num union all
select 13 as num union all
select 14 as num union all
select 15 as num union all
select 16 as num
) t1
;
-- 这个算法更准确
select
t3.cookieid
,t3.createtime
,t3.pv
,t3.percent -- 分位值
,t3.pt --分组内将数据分成N片
,t3.rn
,t3.cn
,t3.rn2
from (
select
t2.cookieid
,t2.createtime
,t2.pv
,t2.pt --分组内将数据分成N片
,t2.rn
,t2.cn
,row_number() over(partition by t2.pt order by t2.pv) as rn2
,ceil(t2.rn / t2.cn * 100) as percent -- 分位值
from (
select
t1.cookieid
,t1.createtime
,t1.pv
,ntile(10) over(order by t1.pv) as pt --分组内将数据分成N片
,row_number() over(order by t1.pv) as rn
,count(1) over() as cn
from (
select 'cookie1' as cookieid ,'2015-04-10' as createtime, 1 as pv union all
select 'cookie1' as cookieid ,'2015-04-11' as createtime, 2 as pv union all
select 'cookie1' as cookieid ,'2015-04-12' as createtime, 3 as pv union all
select 'cookie1' as cookieid ,'2015-04-13' as createtime, 4 as pv union all
select 'cookie1' as cookieid ,'2015-04-14' as createtime, 5 as pv union all
select 'cookie1' as cookieid ,'2015-04-15' as createtime, 6 as pv union all
select 'cookie1' as cookieid ,'2015-04-16' as createtime, 7 as pv union all
select 'cookie2' as cookieid ,'2015-04-10' as createtime, 8 as pv union all
select 'cookie2' as cookieid ,'2015-04-11' as createtime, 9 as pv union all
select 'cookie2' as cookieid ,'2015-04-12' as createtime, 10 as pv union all
select 'cookie2' as cookieid ,'2015-04-13' as createtime, 11 as pv union all
select 'cookie2' as cookieid ,'2015-04-14' as createtime, 12 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 13 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 14 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 15 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 16 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 17 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 18 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 19 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 20 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 21 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 22 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 23 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 24 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 25 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 26 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 27 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 28 as pv union all
select 'cookie2' as cookieid ,'2015-04-15' as createtime, 29 as pv union all
select 'cookie2' as cookieid ,'2015-04-16' as createtime, 30 as pv
) t1
) t2
) t3
where t3.rn2 = 1 or t3.rn = t3.cn
order by t3.rn
;
SQL 十分位的更多相关文章
- AngularJS学习之旅—AngularJS SQL(十二)
一.使用 PHP 从 MySQL 中获取数据 <div ng-app="myApp" ng-controller="customersCtrl"> ...
- CYQ.Data 轻量数据层之路 使用篇-MProc 存储过程与SQL 视频[最后一集] H (二十八)
2019独角兽企业重金招聘Python工程师标准>>> 说明: 本次录制主要为使用篇:CYQ.Data 轻量数据层之路 使用篇五曲 MProc 存储过程与SQL(十六) 的附加视 ...
- Oracle SQL函数之转换函数To_char汇总
TO_CHAR(x[[,c2],C3])[功能]将日期或数据转换为char数据类型[参数]x是一个date或number数据类型.c2为格式参数c3为NLS设置参数如果x为日期nlsparm=NLS_ ...
- oracle_基本SQL语言
一:DDL数据定义语言 1:create(创建) 创建表 CREATE TABLE <table_name>( column1 DATATYPE [NOT NULL] [P ...
- oracle 11g SQL语句补充学习
添加列: alter table tablename add columnName datatype (not null); -------需要注意一点的是在添加一列为非空的时候, 表必 ...
- oracle 常用sql 经典sql函数使用 sql语法
各种树操作, 用来查询表中带有子父节点的信息 Oracle 树操作(select-start with-connect by-prior) select m.org_id from sm_organ ...
- 【函数】Oracle函数系列(2)--数学函数及日期函数
[函数]Oracle函数系列(2)--数学函数及日期函数 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不 ...
- 45个非常有用的oracle语句(摘自尚学堂)
日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. 1 2 SELECT TRUNC (SYSDATE, ...
- Oracle中trunc函数、round 函数、ceil函数和floor函数的使用
Oracle中trunc函数.round 函数.ceil函数和floor函数的使用 1.1trunc函数处理数字 trunc函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定 ...
随机推荐
- [codevs1159]最大全0子矩阵(悬线法)
解题关键:悬线法模板题.注意此模板用到了滚动数组. #include<cstdio> #include<cstring> #include<algorithm> # ...
- GNU Gettext
一.简介 当前,无论是商业还是免费软件都是英文的,并用英文做为文档.直到现在,为使其它非英语语言用户也能够进行交互所做的工作仍然不足,所以这对非英语语言的国家很不利.然而,随着GNU gettext工 ...
- R: 控制流: if & for & while
################################################### 问题:if 判断 18.4.29 if 的应用与??...... 解决方案: # if(){ ...
- Luogu 3676 小清新数据结构题
推荐博客: http://www.cnblogs.com/Mychael/p/9257242.html 感觉还挺好玩的 首先考虑以1为根,把每一个点子树的权值和都算出来,记为$val_{i}$,那么在 ...
- DingTalk机器人C#代码
前面已经介绍了机器人的事情,今天直接贴一下代码. using System; using System.Collections.Generic; using System.ComponentModel ...
- Android 中 吐司显示不出来的原因分析
当你发现你的toast的show方法的确被执行了,但是却没有在界面中显示出来, 有问题的地方可能有两点: 1.Context上下文对象有问题,不是当前的上下文对象或者什么的: 2.message(即T ...
- windows10系统连接蓝牙鼠标自动断开解决方案
环境: Windows 10 企业版 2016 长期服务版 罗技M590 问题: 鼠标长时间未使用,会自动断开 解决步骤: 参考链接: https://zhidao.baidu.com/questio ...
- jQuery+css实现tab功能
点击我我会消失 Click me 点击按钮我会消失,再点击我会出现 演示tab tab1 tab2 tab3 [环球时报记者 郭芳] “中国秘密发射新快速响应火箭”,25日,在中国官方媒体报道我国“快 ...
- 责任型模式一:Observer(观察者)模式
目的: Observer模式的宗旨是在多个对象之间定义一对多的关系,以便当一个对象状态改变时,其他所有依赖于这个对象的对象都能得到通知,并被自动更新.常用于业务逻辑层与表现层的分离. 需求:由GUI引 ...
- C#.Net使用正则表达式抓取百度百家文章列表
工作之余,学习了一下正则表达式,鉴于实践是检验真理的唯一标准,于是便写了一个利用正则表达式抓取百度百家文章的例子,具体过程请看下面源码: 一:获取百度百家网页内容 public List<str ...