博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
presto函数
阅读量:6152 次
发布时间:2019-06-21

本文共 3933 字,大约阅读时间需要 13 分钟。

hot3.png

CASE

CASE expression    WHEN value THEN result    [ WHEN ... ]    [ ELSE result ]ENDSELECT a,       CASE a           WHEN 1 THEN 'one'           WHEN 2 THEN 'two'           ELSE 'many'       END

 

CASE    WHEN condition THEN result    [ WHEN ... ]    [ ELSE result ]ENDSELECT a, b,       CASE           WHEN a = 1 THEN 'aaa'           WHEN b = 2 THEN 'bbb'           ELSE 'ccc'       END

IF

The IF function is actually a language construct that is equivalent to the following CASE expression:

CASE    WHEN condition THEN true_value    [ ELSE false_value ]END

if(conditiontrue_value)

Evaluates and returns true_value if condition is true, otherwise null is returned and true_value is not evaluated.

if(conditiontrue_valuefalse_value)

Evaluates and returns true_value if condition is true, otherwise evaluates and returns false_value.

COALESCE

coalesce(value1value2[, ...])

Returns the first non-null value in the argument list. Like a CASE expression, arguments are only evaluated if necessary.

Window Functions

Window functions perform calculations across rows of the query result. They run after the HAVING clause but before the ORDER BY clause. Invoking a window function requires special syntax using the OVER clause to specify the window. A window has three components:

  • The partition specification, which separates the input rows into different partitions. This is analogous to how the GROUP BY clause separates rows into different groups for aggregate functions.
  • The ordering specification, which determines the order in which input rows will be processed by the window function.
  • The window frame, which specifies a sliding window of rows to be processed by the function for a given row. If the frame is not specified, it defaults to RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. This frame contains all rows from the start of the partition up to the last peer of the current row.

For example, the following query ranks orders for each clerk by price:

SELECT orderkey, clerk, totalprice,       rank() OVER (PARTITION BY clerk                    ORDER BY totalprice DESC) AS rnkFROM ordersORDER BY clerk, rnk

rank() → bigint

Returns the rank of a value in a group of values. The rank is one plus the number of rows preceding the row that are not peer with the row. Thus, tie values in the ordering will produce gaps in the sequence. The ranking is performed for each window partition.

row_number() → bigint

Returns a unique, sequential number for each row, starting with one, according to the ordering of rows within the window partition.

如果  ... over(  ... order by....)中不存在排名相同的,那么rank()和row_number()效果相同,若存在排名相同的,rank()会出现1,1,3,3这样的排名,row_number则是1,2,3,4

Aggregate Functions

All  can be used as window functions by adding the OVER clause. The aggregate function is computed for each row over the rows within the current row’s window frame.

For example, the following query produces a rolling sum of order prices by day for each clerk:

SELECT clerk, orderdate, orderkey, totalprice,       sum(totalprice) OVER (PARTITION BY clerk                             ORDER BY orderdate) AS rolling_sumFROM ordersORDER BY clerk, orderdate, orderkey

830f3126e7f647bfb49e62a45a34d995147.jpg

WITH Clause

The WITH clause defines named relations for use within a query. It allows flattening nested queries or simplifying subqueries. For example, the following queries are equivalent:

SELECT a, bFROM (  SELECT a, MAX(b) AS b FROM t GROUP BY a) AS x;WITH x AS (SELECT a, MAX(b) AS b FROM t GROUP BY a)SELECT a, b FROM x;

This also works with multiple subqueries:

WITH  t1 AS (SELECT a, MAX(b) AS b FROM x GROUP BY a),  t2 AS (SELECT a, AVG(d) AS d FROM y GROUP BY a)SELECT t1.*, t2.*FROM t1JOIN t2 ON t1.a = t2.a;

Additionally, the relations within a WITH clause can chain:

WITH  x AS (SELECT a FROM t),  y AS (SELECT a AS b FROM x),  z AS (SELECT b AS c FROM y)SELECT c FROM z;

 

转载于:https://my.oschina.net/hutaishi/blog/3005415

你可能感兴趣的文章
Zabbix基于Proxy分布式部署实现Web监控
查看>>
python版春节倒计时实时显示
查看>>
CentOS 6.5下dhcp服务器搭建与配置
查看>>
Python 处理 Excel
查看>>
多态要满足三个条件
查看>>
java heap size:设定运行时的堆大小
查看>>
kickstart命令选项
查看>>
java --List<Object>去重
查看>>
linux shell下除了某个文件外的其他文件全部删除的命令
查看>>
Kendo UI常用示例汇总(二十)
查看>>
七.网络
查看>>
1.网站综合***_Discuz!
查看>>
我的友情链接
查看>>
《Inside C#》笔记(六) 属性、数组、索引器
查看>>
想让容器更快?这五种方法您必须知道!
查看>>
linux 学习第二次测验
查看>>
想转行做通讯电子类技术文档翻译,请推荐需要学习什么计算机软件
查看>>
Apache MINA --- [使用JMX来管理MINA应用]
查看>>
c++11 的Thread测试。
查看>>
网络通信安全基础和OpenSSL
查看>>