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函数极为类似,只是该函数不对指定 ...
随机推荐
- 使用Java建立聊天客户端
---------------siwuxie095 关于 聊天服务器,详见本人博客的分类:来一杯Java, 里面的 使用ServerSocket ...
- Django扩展Auth-User表的几种方法
方式1, OneToOneField from django.contrib.auth.models import Userclass UserProfile(models.Model): user ...
- leetCode130. Surrounded Regions--广度优先遍历算法
Problem: Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by ' ...
- HtmlAgilityPack HTML操作类库的使用
HtmlAgilityPack是.NET下的一个开源的HTML解析类库.支持用XPath来解析HTML.命名空间:HtmlAgilityPack. 1.读取网络中html网页内容,获取网页中元素bod ...
- LeetCode第617题:合并二叉树
问题描述 解题思路 略略略略略 C++代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tree ...
- 《Head First Servlets & JSP》-13-过滤器和包装器
过滤器是什么 与servlet非常类似,过滤器就是java组件,请求发送到servlet之前,可以用过滤器截获和处理清求,另外 servlet结束工作之后,在响应发回给客户之前,可以用过滤器处理响应. ...
- [译]我们应该在HTML文档中何处放script标签
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- javascript 获取iframe元素的方法
javascript 获取iframe元素的方法 第一种: $("#IframeID").contents().find("div"); 第二种: $(win ...
- Lucene 全文检索 Lucene的使用
Lucene 全文检索 Lucene的使用 一.简介: 参考百度百科: http://baike.baidu.com/link?url=eBcEVuUL3TbUivRvtgRnMr1s44nTE7 ...
- 使用pip安装离线包
为了方便以后查看,特总结于此: 下载离线安装包并放到你想放的文件目录下 使用anaconda prompt安装离线文件 如果没有安装anaconda,则参照下边链接里边的操作!!! 离线环境通过pip ...