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函数极为类似,只是该函数不对指定 ...
随机推荐
- JS中的!= 、== 、!==、===的用法和区别
与c++中每一种类型都有明确的的定义不同:因JS中var定义存在,未具体区分类型,!=与==不能包含所有的条件,故加入!==与===用法: var num = 1; var str = '1'; va ...
- java开发中用到的技术(持续更新.....)
一.数据库 1.数据库连接池:当jdbc连接数据库使用DriverManager 获取时,每次向数据库建立连接的时候都要讲connection加载到内存中,当同时使用的用户数量较大时,会造成服务器不堪 ...
- Linux问题FAQ1
1.使用vi编辑器时候,按方向键会产生A,B,C之类的 解决办法:ubuntu server 8.04, vim版本为 7.1.138,客户端使用pietty.vim 在插入模式下, 方向键被转为A ...
- Java接口基础
接口(interface) 1.接口体中包含常量的声明(没有变量)和抽象方法两部分.接口体中只有抽象方法,没有普通的方法,而且接口体中所有的常量访问权限一定是public,而且是static常量(允许 ...
- grep多条件和sed合并两行
grep多条件,最简单的方式是使用正则表达式: egrep "条件1|条件2" sed合并两行,将 1 2 3 4 合并成: 1 2 3 4 sed '$!N;s/\n/\t/ ...
- delphi xe6 调用java GPS的方法
如果用xe6自带的LocationSensor控件,默认优先使用网络位置,网络位置定位精度不准确,不能满足高精度定位的要求.但xe6自带的LocationSensor控件不能指定网络定位优先还是GPS ...
- BCP导入导出
- android中SharedPreferences 读取不到数据的问题
在两个不同的 Activity中,A中SharedPreferences保存了数据,在A中可以读取到,但是在 B中却读取不到了,一直是空值,好是不爽,由于是按照书本上的例子写的, 怎么也找不到原因,后 ...
- File类和Directory类
File类和Directory类分别用来对文件和各种目录进行操作,这两类可以被实例化,但不能被其他类集成. 1. File类(静态类) File类支持对文件的基本操作,它包括用于创建.复制.删除.移动 ...
- c++滚动数组
说来惭愧,我老早以前就学习了dp,可直到最近才知道滚动数组. 所以说,滚动数组是什么呢? 它是一种优化dp空间复杂度的思想. 在dp转移时,我们往往不需要之前推的所有的,而是只需要前一两个转移的. 我 ...