SAPHANA学习(23):SQL Function(W)

Posted 渔歌晚唱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SAPHANA学习(23):SQL Function(W)相关的知识,希望对你有一定的参考价值。

/*

149.WEEK

WEEK(<date>)

返回周数

 

WEEKDAY(<date>)

返回日期所在周天,星期1(0)~星期天(6)

*/

SELECT  WEEK(TO_DATE(2011-05-30, YYYY-MM-DD))  FROM DUMMY;
SELECT  WEEK (2017-01-02) FROM DUMMY;
SELECT WEEKDAY (TO_DATE (2010-12-31, YYYY-MM-DD)) "week day" FROM DUMMY;

/*

150. WEIGHTED_AVG

WEIGHTED_AVG(<expression>) <window_specification>

计算规则:

weight(<i>) = 2 * (<n> + 1 - <i>) / (<n> * (<n> + 1))

 

*/

CREATE ROW TABLE TEST_WEIGHT (station INT, ts DATE, temperature FLOAT);
INSERT INTO TEST_WEIGHT VALUES(1, 2014-01-01, 0.0);
INSERT INTO TEST_WEIGHT VALUES(1, 2014-01-02, 3.0);
INSERT INTO TEST_WEIGHT VALUES(1, 2014-01-03, 4.5);
INSERT INTO TEST_WEIGHT VALUES(2, 2014-01-04, 6.0);
INSERT INTO TEST_WEIGHT VALUES(2, 2014-01-05, 6.3);

SELECT
       ts,
       temperature,
       WEIGHTED_AVG(temperature)
       OVER (ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
       FROM TEST_WEIGHT ORDER BY ts;

/*

151. WIDTH_BUCKET

WIDTH_BUCKET(<expression>, <min_value>, <max_value>, <num_buckets>)

<expression>数字类型或者时间类型,或者能够转换成的类型

<min_value>

<max_value>

<num_buckets>

*/

CREATE TABLE TEST_WIDTH_BUCKET (NUM INT);
INSERT INTO TEST_WIDTH_BUCKET VALUES(1);
INSERT INTO TEST_WIDTH_BUCKET VALUES(2);
INSERT INTO TEST_WIDTH_BUCKET VALUES(3);
INSERT INTO TEST_WIDTH_BUCKET VALUES(4);
INSERT INTO TEST_WIDTH_BUCKET VALUES(5);

--不存在?
--SELECT *, WIDTH_BUCKET( NUM, 2, 5, 2 ) FROM TEST_WIDTH_BUCKET;

/*

152. WORKDAYS_BETWEEN

WORKDAYS_BETWEEN(<factory_calendar_id>, <start_date>, <end_date> [, <source_schema>])

计算开始日期和结束日期之间的工作日数

<factory_calendar_id> ::= <string_literal>

工厂日历ID,工厂日历表:TFACS

<start_date> ::= <string_literal> | <DATE>

<end_date> ::= <string_literal> | <DATE>

<source_schema> ::= <string_literal>

TFACS所在schema

 

如果<start_date>小于<end_date>,返回之间工作日,包括<start_date>,不包括<end_date>

如果<start_date>大于<end_date>,返回负数工作日,包括<end_date>,不包括<start_date>

*/

--SELECT WORKDAYS_BETWEEN(‘01‘, ‘2014-01-09‘, ‘2014-01-10‘ , ‘FCTEST‘) FROM DUMMY;

 

以上是关于SAPHANA学习(23):SQL Function(W)的主要内容,如果未能解决你的问题,请参考以下文章

SAPHANA学习(26):SQL Function 分类汇总

SAPHANA学习:SQL Function(B)

SAPHANA学习(22):SQL Function(V)

SAPHANA学习(21):SQL Function(U)

SAPHANA学习(24):SQL Function(X)

SAPHANA学习(20):SQL Function(T)