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;

 

上一篇:【WC2013】糖果公园(树上带修莫队)


下一篇:苹果网址一键免签绿标打包工具