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函数极为类似,只是该函数不对指定 ...
随机推荐
- cocos2d-js 骨骼动画 3.10
近期使用了cocos动画中的骨骼动画,这里记录使用的两种方式(3.10版): 一.cocos自带的动画编辑器导出的动画 ccs.armatureDataManager.addArmatureFileI ...
- wenfrom的简单控件和repeater控件
简单控件 lable 转换成<span>标记 literal 空的 什么也没转换 Literal.Text=<script>alter('你好');</scrip ...
- php单引号双引号的区别
单引号里面的内容是直接被当做一个字符串,用双引号定义的字符串的内容最只要的特征就是会被解析.
- 【转】PEAR安装、管理及使用
PEAR安装 linux下只要你安装的是PHP 4.3.0以上的版本,默认安装都是支持PEAR的,除非你使用了”--WITHOUT-PEAR”选项,修改PHP.INI文件,在INCLUDE_PAT ...
- centos 6.5安装 redis
版本:redis-2.8.19.tar.gz 检查下面依赖是否安装,如果没有要先安装,不然会有异常. yum install gcc-c++ yum install -y tcl. .获取安装文件 r ...
- SDUT 3376 数据结构实验之查找四:二分查找
数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...
- spark 1.5的hivecontext的问题
spark升级到1.5,里面的hive版本升级到1.2.1版本,我写了如下的代码 object SQLApp extends App{ val sparkconf = new SparkConf(). ...
- scala中的注解
scala中很多注解实现java中关键字的用法 @volatile注解标记为易失的:@transient注解将字段标记为瞬态的:@strictfp注解对应strictfp修饰符:@native注解标记 ...
- php 函数追踪扩展 phptrace
php 函数追踪扩展 phptrace 介绍 phptrace 是一个低开销的用于跟踪.分析 php 运行情况的工具. 它可以跟踪 php 在运行时的函数调用.请求信息.执行流程.并且提供有过滤器.统 ...
- Laradock使用教程(新手版)
Laradock使用教程 背景 最近我们公司把开发环境从windows系统换到了Ubuntu系统.用windows系统的时候,我们一般用phpStudy集成环境的比较多.换到Linux环境下,我们选择 ...