SQL Window Function

本文介绍了SQL中的窗口函数,包括PARTITION BY子句的使用,如何创建运行总和,以及ROW_NUMBER和RANK函数在排名中的应用。内容涵盖按年分组的运行总和计算,按账户对订单总额进行排名,以及不同account_id下的聚合分析。
摘要由CSDN通过智能技术生成

Window Function 1

PARTITION BY Clause

A PARTITION BY clause is used to partition rows of table into groups.

Syntax:

Window_function ( expression ) 
       Over ( partition by expr [order_clause] [frame_clause] ) 

order_clause and frame_clause are optional.
expr can be column names or built-in functions in MySQL.
But, standard SQL permits only column names in expr.

over() clause defines how to partition and order rows of table, which is to be processed by window function rank().

Running Total

select standard_qty,
		   date_trunc('month', occurred_at) as month,
		   sum(standard_qty) over (partition by date_trunc('month', occurred_at) order by occurred_at) as running_total
from orders;
  1. Using Derek’s previous video as an example, create another running total. This time, create a running total of standard_amt_usd (in the orders table) over order time with no date truncation. Your final table should have two columns: one with the amount being added for each new row, and a second with the running total.
SELECT standard_amt_usd,
       SUM(standard_amt_usd) OVER (ORDER BY occurred_at
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值