SAPHANA学习:SQL Function(B)

Posted 渔歌晚唱

tags:

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

/*

21.BINNING Function

Partitions an input set into disjoint subsets by assigning a bin number to each row.

BINNING( <binning_param> => <expression> [ {, <binning_parameter> => <expression> } ... ] ) <window_specification>

<binning_param> ::= VALUE | BIN_COUNT | BIN_WIDTH | TILE_COUNT | STDDEV_COUNT

VALUE is always required.

It specifies the column that binning is applied to.

When BIN_WIDTH is used, the input column must have a numeric data type.

BIN_COUNT specifies the number of equal-width bins.

BIN_WIDTH specifies the width of the bins.

TILE_COUNT specifies the number of bins with equal number of records.

STDDEV_COUNT specifies the number of standard deviations left and right from the mean.

The appropriate binning method is selected based on the parameter specified – exactly one of the last four parameters must be non-NULL.

The value assigned to binning method parameter must be an integer expression.

*/

CREATE ROW TABLE weather (station INT, ts DATE, temperature FLOAT);
INSERT INTO weather VALUES(1, 2014-01-01, 0);
INSERT INTO weather VALUES(1, 2014-01-02, 3);
INSERT INTO weather VALUES(1, 2014-01-03, 4.5);
INSERT INTO weather VALUES(1, 2014-01-04, 6);
INSERT INTO weather VALUES(1, 2014-01-05, 6.3);
INSERT INTO weather VALUES(1, 2014-01-06, 5.9);
INSERT INTO weather VALUES(1, 2015-01-01, 1);
INSERT INTO weather VALUES(1, 2015-01-02, 3.4);
INSERT INTO weather VALUES(1, 2015-01-03, 5);
INSERT INTO weather VALUES(1, 2015-01-04, 6.7);
INSERT INTO weather VALUES(1, 2015-01-05, 4.6);
INSERT INTO weather VALUES(1, 2015-01-06, 6.9);

--OVER子句不能指定 <window_order_by_clause>,也不能指定任何窗口框架,因为binning函数在整个分区上工作。
SELECT *,
       BINNING(
       VALUE => temperature,
       BIN_COUNT => 4)
       OVER () AS bin_num FROM weather;

/*

22.BINTOHEX Function

BINTOHEX(<expression>)

将二进制值转换为VARCHAR数据类型十六进制值。如果输入值不是二进制值,则首先将其转换为二进制值。

23.BINTONHEX Function

BINTONHEX(<expression>)

将二进制值转换为NVARCHAR数据类型十六进制值。如果输入值不是二进制值,则首先将其转换为二进制值。

24.BINTOSTR

BINTOSTR(<varbinary_string>)

Converts a VARBINARY string <varbinary_string> to a character string with CESU-8 encoding.

*/

SELECT BINTOHEX(AB) FROM DUMMY;
SELECT BINTONHEX(AB) FROM DUMMY;
SELECT BINTOSTR (416E74) FROM DUMMY;

/*

25.BITAND Function

BITAND(<value1>, <value2>)

按位与操作

<value1>,<value2>必须是非负整数,VARBINARY类型

BITAND会将输入字符串类型值转换为BIGINT,BITOR,BITXOR,BITNOT Function转换为INT

26.BITCOUNT Function

BITCOUNT(<expression>)

<expression>必须是整数或VARBINARY类型

返回整型数据,位数

27.BITNOT Function

BITNOT(<expression>)

按位执行非操作

28.BITOR Function

BITOR(<expression1>, <expression2>)

<expression1>,<expression2>必须非负整数或VARBINARY类型

按位执行或操作

29.BITXOR Function

BITXOR(<expression1>, <expression2>)

按位执行异或操作

<expression1>, <expression2>必须是非负整数或VARBINARY类型

30.BITSET Function

BITSET(<target_num>, <start_bit>, <num_to_set>)

Sets a specific number of bits to 1 in a target number from a specified 1-based index position.

<target_num> ::= <string_literal>

The VARBINARY number where the bits are to be set.

<start_bit> ::= <unsigned_integer>

A 1-based index position where the first bit is to be set.

<num_to_set> ::= <unsigned_integer>

The number of bits to be set in the target number.

31.BITUNSET Function

BITUNSET(<target_num>, <start_bit>, <num_to_unset>)

Sets a specified number of bits to 0 in a target number from a specified 1-based index position.

*/

SELECT BITAND (255, 123)  FROM DUMMY;
SELECT BITCOUNT (255) FROM DUMMY;
SELECT BITNOT (255) FROM DUMMY;
SELECT BITOR (255, 123) FROM DUMMY;

--255=>1111 1111   123=>0111 1011,进行异或运算 1000 0100
SELECT BITXOR (255, 123) FROM DUMMY;
SELECT BITSET (1111, 1, 3) FROM DUMMY;
SELECT BITUNSET (ffff, 1, 3) FROM DUMMY;

 

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

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

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

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

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

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

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