当前位置: 首页 > news >正文

厦门专业网站设计微信卖货小程序怎么做

厦门专业网站设计,微信卖货小程序怎么做,搭建起什么样的平台,wordpress 指定页面5个小案例说清楚-窗口函数(开窗函数) 结论先行: 首先,写开窗函数一定要先理解开窗的定义和为什么开窗,开窗分几种场景; 写sql,需要心平气和,一步一步进行推导,这样可以找到写sql的逻…

5个小案例说清楚-窗口函数(开窗函数)

结论先行:

首先,写开窗函数一定要先理解开窗的定义和为什么开窗,开窗分几种场景;

写sql,需要心平气和,一步一步进行推导,这样可以找到写sql的逻辑,最终达到万变不离其宗。

over(): 就是为每条数据都开启一个窗口. 窗口的大小默认为当前数据集的大小.
over(partition by…): 会按照分区字段将数据分到不同的区中.
会为每个分区中的每条数据都开启一个窗口.
窗口的大小默认为当前分区数据集的大小
over(order by …): 会为每条数据都开启一个窗口,窗口的大小默认为起点到当前行
over(rows between … and …) :划分窗口大小
over(partition by … order by … ) :
会按照分区字段将数据分到不同的区中.
会为每个分区中的每条数据都开启一个窗口.
窗口的大小默认为当前分区数据集中起点到当前行

1.相关函数说明

over():指定分析函数工作的数据窗口大小,也就是可操作的数据集大小,这个数据窗口大小可能会随着行的改变而变化;
current row:当前行
n preceding:往前n行数据
n following:往后n行数据
unbounded:起点
unbounded preceding:表示到前面的起点
unbounded following:表示到后面的重点
lag(col, n, default_val):往前第n行数据
lead(col, n, default_val):往后第n行数据
ntile(n):把有序窗口的行分发到指定数据的组中,各个组有编号,编号从1开始,对于每一行,ntile返回此行所属的组的编号。注意:n必须为int类型

2.准备表和数据:
jack,2017-01-01,10
tony,2017-01-02,15
jack,2017-02-03,23
tony,2017-01-04,29
jack,2017-01-05,46
jack,2017-04-06,42
tony,2017-01-07,50
jack,2017-01-08,55
mart,2017-04-08,62
mart,2017-04-09,68
neil,2017-05-10,12
mart,2017-04-11,75
neil,2017-06-12,80
mart,2017-04-13,94
--创建表
create table business(
name string, 
orderdate string,
cost int
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
--导入数据
load data local inpath "/opt/module/hive-3.1.2/datas/business.txt" into table business;
需求一:

查询在2017年4月份购买过的顾客及总人数

分步分析得出:
a)查询2017年4月份的数据
b)查询购买过的顾客名称
c)求总人数

这里用到了开窗函数over()表示在所有的当前数据集中。

注意:要一步一步分析,切记上来囫囵吞枣,没有搞清楚需求,就开始写

--a)查询2017年4月份的数据
selectname,orderdate,cost
frombusiness
where orderdate like '2017-04%'; =>t1
+-------+-------------+-------+
| name  |  orderdate  | cost  |
+-------+-------------+-------+
| jack  | 2017-04-06  | 42    |
| mart  | 2017-04-08  | 62    |
| mart  | 2017-04-09  | 68    |
| mart  | 2017-04-11  | 75    |
| mart  | 2017-04-13  | 94    |
+-------+-------------+-------+
-- b)查询购买过的顾客名称
selectdistinct t1.name
fromt1 ; => t2
-- c)求总人数
selectt2.name,count(t2.name) over() total_num
fromt2 ; 
-- d)组装
selectt2.name,count(t2.name) over() total_num
from(
selectdistinct t1.name
from(
selectname,orderdate,cost
frombusiness
where orderdate like '2017-04%')t1 )t2 ; --结果可得
+----------+------------+
| t2.name  | total_num  |
+----------+------------+
| mart     | 2          |
| jack     | 2          |
+----------+------------+
需求二:

查询顾客的购买明细及月购买总额

分析:
每行的窗口数据为该月份本人的购买次数
所以需要按照月份进行分区
需要用到 over(partition by monthNum)

此题可以扩展成两个题;

扩展1

查询顾客的购买明细及所有顾客月购买总额

selectname,orderdate,cost,sum(cost) over(partition by month(orderdate)) total_cost
frombusiness ;
--结果可得
+-------+-------------+-------+-------------+
| name  |  orderdate  | cost  | total_cost  |
+-------+-------------+-------+-------------+
| jack  | 2017-01-01  | 10    | 205         |
| jack  | 2017-01-08  | 55    | 205         |
| tony  | 2017-01-07  | 50    | 205         |
| jack  | 2017-01-05  | 46    | 205         |
| tony  | 2017-01-04  | 29    | 205         |
| tony  | 2017-01-02  | 15    | 205         |
| jack  | 2017-02-03  | 23    | 23          |
| mart  | 2017-04-13  | 94    | 341         |
| jack  | 2017-04-06  | 42    | 341         |
| mart  | 2017-04-11  | 75    | 341         |
| mart  | 2017-04-09  | 68    | 341         |
| mart  | 2017-04-08  | 62    | 341         |
| neil  | 2017-05-10  | 12    | 12          |
| neil  | 2017-06-12  | 80    | 80          |
+-------+-------------+-------+-------------+
扩展1

查询顾客的购买明细及每个顾客的月购买总额

selectname,orderdate,cost,sum(cost) over(partition by name, month(orderdate)) total_cost 
frombusiness ;
--查询结果
+-------+-------------+-------+-------------+
| name  |  orderdate  | cost  | total_cost  |
+-------+-------------+-------+-------------+
| jack  | 2017-01-05  | 46    | 111         |
| jack  | 2017-01-08  | 55    | 111         |
| jack  | 2017-01-01  | 10    | 111         |
| jack  | 2017-02-03  | 23    | 23          |
| jack  | 2017-04-06  | 42    | 42          |
| mart  | 2017-04-13  | 94    | 299         |
| mart  | 2017-04-11  | 75    | 299         |
| mart  | 2017-04-09  | 68    | 299         |
| mart  | 2017-04-08  | 62    | 299         |
| neil  | 2017-05-10  | 12    | 12          |
| neil  | 2017-06-12  | 80    | 80          |
| tony  | 2017-01-04  | 29    | 94          |
| tony  | 2017-01-02  | 15    | 94          |
| tony  | 2017-01-07  | 50    | 94          |
+-------+-------------+-------+-------------+
14 rows selected (20.778 seconds)
需求三:
求每个顾客的购买明细及将每个顾客的cost按照日期进行累加分析:窗口函数用到了order by 和 rows between unbounded preceding and current row
selectname,orderdate,cost,sum(cost) over(order by orderdate rows between unbounded preceding and current row)
frombusiness ;
-- 查询结果
+-------+-------------+-------+---------------+
| name  |  orderdate  | cost  | sum_window_0  |
+-------+-------------+-------+---------------+
| jack  | 2017-01-01  | 10    | 10            |
| tony  | 2017-01-02  | 15    | 25            |
| tony  | 2017-01-04  | 29    | 54            |
| jack  | 2017-01-05  | 46    | 100           |
| tony  | 2017-01-07  | 50    | 150           |
| jack  | 2017-01-08  | 55    | 205           |
| jack  | 2017-02-03  | 23    | 228           |
| jack  | 2017-04-06  | 42    | 270           |
| mart  | 2017-04-08  | 62    | 332           |
| mart  | 2017-04-09  | 68    | 400           |
| mart  | 2017-04-11  | 75    | 475           |
| mart  | 2017-04-13  | 94    | 569           |
| neil  | 2017-05-10  | 12    | 581           |
| neil  | 2017-06-12  | 80    | 661           |
+-------+-------------+-------+---------------+

扩展需求:

求每个顾客的购买明细及
起点到当前行的累加
上一行到当前行的累加
当前行到下一行的累加
上一行到下一行的累加
当前行到终点的累加

selectname,orderdate,cost,sum(cost) over(order by orderdate rows between unbounded preceding and current row) up_c,sum(cost) over(order by orderdate rows between 1 preceding and current row) 1p_c,sum(cost) over(order by orderdate rows between current row and 1 following) c_1f,sum(cost) over(order by orderdate rows between 1 preceding and 1 following) 1p_1f,sum(cost) over(order by orderdate rows between current row and unbounded following) c_uf
frombusiness;
--执行结果
+-------+-------------+-------+-------+-------+-------+--------+-------+
| name  |  orderdate  | cost  | up_c  | 1p_c  | c_1f  | 1p_1f  | c_uf  |
+-------+-------------+-------+-------+-------+-------+--------+-------+
| jack  | 2017-01-01  | 10    | 10    | 10    | 25    | 25     | 661   |
| tony  | 2017-01-02  | 15    | 25    | 25    | 44    | 54     | 651   |
| tony  | 2017-01-04  | 29    | 54    | 44    | 75    | 90     | 636   |
| jack  | 2017-01-05  | 46    | 100   | 75    | 96    | 125    | 607   |
| tony  | 2017-01-07  | 50    | 150   | 96    | 105   | 151    | 561   |
| jack  | 2017-01-08  | 55    | 205   | 105   | 78    | 128    | 511   |
| jack  | 2017-02-03  | 23    | 228   | 78    | 65    | 120    | 456   |
| jack  | 2017-04-06  | 42    | 270   | 65    | 104   | 127    | 433   |
| mart  | 2017-04-08  | 62    | 332   | 104   | 130   | 172    | 391   |
| mart  | 2017-04-09  | 68    | 400   | 130   | 143   | 205    | 329   |
| mart  | 2017-04-11  | 75    | 475   | 143   | 169   | 237    | 261   |
| mart  | 2017-04-13  | 94    | 569   | 169   | 106   | 181    | 186   |
| neil  | 2017-05-10  | 12    | 581   | 106   | 92    | 186    | 92    |
| neil  | 2017-06-12  | 80    | 661   | 92    | 80    | 92     | 80    |
+-------+-------------+-------+-------+-------+-------+--------+-------+
14 rows selected (17.403 seconds)
需求四:

查询每个顾客上次 和 下次 的购买时间

分析:
lag(col, n, default_val):往前第n行数据
lead(col, n, default_val):往后第n行数据

selectname,orderdate,cost,lag(orderdate, 1, '1977-01-01') over(partition by name order by orderdate) pre_ord,lead(orderdate, 1, '9999-01-01') over(partition by name order by orderdate) nex_ord
frombusiness;
--结果
+-------+-------------+-------+-------------+-------------+
| name  |  orderdate  | cost  |   pre_ord   |   nex_ord   |
+-------+-------------+-------+-------------+-------------+
| jack  | 2017-01-01  | 10    | 1977-01-01  | 2017-01-05  |
| jack  | 2017-01-05  | 46    | 2017-01-01  | 2017-01-08  |
| jack  | 2017-01-08  | 55    | 2017-01-05  | 2017-02-03  |
| jack  | 2017-02-03  | 23    | 2017-01-08  | 2017-04-06  |
| jack  | 2017-04-06  | 42    | 2017-02-03  | 9999-01-01  |
| mart  | 2017-04-08  | 62    | 1977-01-01  | 2017-04-09  |
| mart  | 2017-04-09  | 68    | 2017-04-08  | 2017-04-11  |
| mart  | 2017-04-11  | 75    | 2017-04-09  | 2017-04-13  |
| mart  | 2017-04-13  | 94    | 2017-04-11  | 9999-01-01  |
| neil  | 2017-05-10  | 12    | 1977-01-01  | 2017-06-12  |
| neil  | 2017-06-12  | 80    | 2017-05-10  | 9999-01-01  |
| tony  | 2017-01-02  | 15    | 1977-01-01  | 2017-01-04  |
| tony  | 2017-01-04  | 29    | 2017-01-02  | 2017-01-07  |
| tony  | 2017-01-07  | 50    | 2017-01-04  | 9999-01-01  |
+-------+-------------+-------+-------------+-------------+
需求五:
需求五: 查询前20%时间的订单信息分析:ntile(n):把有序窗口的行分发到指定数据的组中,各个组有编号,编号从1开始,对于每一行,ntile返回此行所属的组的编号。注意:n必须为int类型
--将数据分成5组
selectname,orderdate,cost,ntile(5) over(order by orderdate) gid
frombusiness ; =>t1
--取第一组的数据
selectt1.name,t1.orderdate,t1.cost
fromt1
where t1.gid = 1;
--组合
selectt1.name,t1.orderdate,t1.cost
from(
selectname,orderdate,cost,ntile(5) over(order by orderdate) gid
frombusiness )t1 where t1.gid = 1 ;
-- 结果
+----------+---------------+----------+
| t1.name  | t1.orderdate  | t1.cost  |
+----------+---------------+----------+
| jack     | 2017-01-01    | 10       |
| tony     | 2017-01-02    | 15       |
| tony     | 2017-01-04    | 29       |
+----------+---------------+----------+

文章转载自:
http://endoglobular.mzpd.cn
http://difunctional.mzpd.cn
http://sleepcoat.mzpd.cn
http://meadowy.mzpd.cn
http://electromyogram.mzpd.cn
http://hereof.mzpd.cn
http://keeno.mzpd.cn
http://nalorphine.mzpd.cn
http://cattlelifter.mzpd.cn
http://tgif.mzpd.cn
http://female.mzpd.cn
http://contrapose.mzpd.cn
http://exhilaratingly.mzpd.cn
http://midnightly.mzpd.cn
http://starchy.mzpd.cn
http://richard.mzpd.cn
http://olap.mzpd.cn
http://plainclothes.mzpd.cn
http://reveille.mzpd.cn
http://cicala.mzpd.cn
http://freesia.mzpd.cn
http://volubile.mzpd.cn
http://bootload.mzpd.cn
http://sorta.mzpd.cn
http://blastocyst.mzpd.cn
http://picotite.mzpd.cn
http://dealfish.mzpd.cn
http://eyewash.mzpd.cn
http://semiarc.mzpd.cn
http://kerogen.mzpd.cn
http://cessative.mzpd.cn
http://lousiness.mzpd.cn
http://bierstube.mzpd.cn
http://intercept.mzpd.cn
http://bolix.mzpd.cn
http://engrave.mzpd.cn
http://offending.mzpd.cn
http://galenoid.mzpd.cn
http://limerick.mzpd.cn
http://mithridatism.mzpd.cn
http://extremely.mzpd.cn
http://kodak.mzpd.cn
http://morphoneme.mzpd.cn
http://date.mzpd.cn
http://jesus.mzpd.cn
http://reformism.mzpd.cn
http://mashy.mzpd.cn
http://burr.mzpd.cn
http://cob.mzpd.cn
http://impluvium.mzpd.cn
http://pox.mzpd.cn
http://anhemitonic.mzpd.cn
http://loudhailer.mzpd.cn
http://tosspot.mzpd.cn
http://dithered.mzpd.cn
http://ependyma.mzpd.cn
http://acquiescently.mzpd.cn
http://geoanticline.mzpd.cn
http://irides.mzpd.cn
http://irrationalism.mzpd.cn
http://reposefully.mzpd.cn
http://decarbonylate.mzpd.cn
http://fellowlike.mzpd.cn
http://mucociliary.mzpd.cn
http://laundryman.mzpd.cn
http://intranet.mzpd.cn
http://ddk.mzpd.cn
http://jewess.mzpd.cn
http://granulomatosis.mzpd.cn
http://lessness.mzpd.cn
http://fluoresce.mzpd.cn
http://reverently.mzpd.cn
http://mousaka.mzpd.cn
http://coatee.mzpd.cn
http://byrd.mzpd.cn
http://uncandid.mzpd.cn
http://irrelevant.mzpd.cn
http://hemangioma.mzpd.cn
http://triadelphous.mzpd.cn
http://quizzer.mzpd.cn
http://waveform.mzpd.cn
http://birdy.mzpd.cn
http://deckie.mzpd.cn
http://trimolecular.mzpd.cn
http://stridulant.mzpd.cn
http://gaikwar.mzpd.cn
http://witless.mzpd.cn
http://localitis.mzpd.cn
http://professed.mzpd.cn
http://phlegmatized.mzpd.cn
http://unorganized.mzpd.cn
http://acrodynia.mzpd.cn
http://anaconda.mzpd.cn
http://bulwark.mzpd.cn
http://aileen.mzpd.cn
http://eating.mzpd.cn
http://realpolitik.mzpd.cn
http://appellor.mzpd.cn
http://loverboy.mzpd.cn
http://modificative.mzpd.cn
http://www.15wanjia.com/news/85957.html

相关文章:

  • 深圳网站定制开发seo如何优化关键词上首页
  • 集团公司网站源码php在百度上怎么发布信息
  • 网站更新怎么做十大网络营销经典案例
  • 公司网页设计步骤百度seo2022
  • 安州区建设局网站网络营销培训
  • 西安网站建设管理广州今日刚刚发生的新闻
  • 怎么做论坛的网站专业软文平台
  • 天津外贸网站建设清远今日头条最新消息
  • 在西安建设工程交易中心网站广州新闻热点事件
  • 扬州网站建设推广经典软文案例100例
  • 泗洪房产网哈尔滨seo优化软件
  • 网上书店网站前端搜索条怎么做如何进行关键词分析
  • 做门户网站用什么软件网址seo优化排名
  • 网站搭建大型公司培训教育机构
  • 集宁做网站关键词com
  • 备案 网站负责人 法人全案网络推广公司
  • 公司支付网站服务费怎么做分录任务放单平台
  • 官方网站管理办法手机网站
  • 深圳建设企业网站营销技巧和营销方法心得
  • 建设一个小说网站成功的营销案例及分析
  • 电商平台运营是做什么的seo关键词排名优化教程
  • wordpress 不显示全文百度seo收录软件
  • 电商网站大连安庆seo
  • 保定网站制作报价网站seo外包靠谱吗
  • 如何做网站旅游产品分析成都建设网官网
  • 有没有免费b2b平台咸阳seo
  • 做网站的个人心得百度热搜榜第一
  • php做的大型网站全媒体广告策划营销
  • 澳门seo推广360优化大师软件
  • 专业旅游网站建设企业建站 平台