SnowConvert AI - Redshift - Expressions¶
表达式列表¶
描述¶
表达式列表是表达式的组合,可以出现在成员资格和比较条件(WHERE 子句)以及 GROUP BY 子句中。(Redshift SQL 语言参考“表达式列表” (https://docs.aws.amazon.com/redshift/latest/dg/r_expression_lists.html))。
Note
Snowflake 完全支持此语法。
语法¶
示例源模式¶
Setup data¶
Redshift¶
IN 子句¶
输入代码:¶
Redshift¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
2 |
Bob |
5 |
banana |
200 |
3 |
Charlie |
10 |
cherry |
300 |
输出代码:¶
Snowflake¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
2 |
Bob |
5 |
banana |
200 |
3 |
Charlie |
10 |
cherry |
300 |
比较¶
输入代码:¶
Redshift¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
输出代码:¶
Snowflake¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
备注
在 Snowflake 中,使用以下运算符进行表达式列表比较可能会有不同的行为:(< , <= , > , >=)。这些运算符会转换为逻辑 AND 运算,以在 Snowflake 中实现完全等效。
输入代码:¶
Redshift¶
Result¶
R1 |
R2 |
R3 |
R4 |
R5 |
|---|---|---|---|---|
FALSE |
FALSE |
NULL |
NULL |
FALSE |
输出代码:¶
Snowflake¶
Result¶
R1 |
R2 |
R3 |
R4 |
R5 |
|---|---|---|---|---|
FALSE |
FALSE |
NULL |
NULL |
FALSE |
嵌套元组¶
输入代码:¶
Redshift¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
2 |
Bob |
5 |
banana |
200 |
3 |
Charlie |
10 |
cherry |
300 |
输出代码¶
Snowflake¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
2 |
Bob |
5 |
banana |
200 |
3 |
Charlie |
10 |
cherry |
300 |
Case 语句¶
输入代码:¶
Redshift¶
Result¶
RESULT |
|---|
Found |
Found |
Found |
Not Found |
Not Found |
Not Found |
输出代码¶
Snowflake¶
Result¶
RESULT |
|---|
Found |
Found |
Found |
Not Found |
Not Found |
Not Found |
多个表达式¶
输入代码:¶
Redshift¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
2 |
Bob |
5 |
banana |
200 |
3 |
Charlie |
10 |
cherry |
300 |
输出代码¶
Snowflake¶
Result¶
ID |
NAME |
QUANTITY |
FRUIT |
PRICE |
|---|---|---|---|---|
1 |
Alice |
1 |
apple |
100 |
2 |
Bob |
5 |
banana |
200 |
3 |
Charlie |
10 |
cherry |
300 |
联接¶
输入代码:¶
Redshift¶
Result¶
QUANTITY |
FRUIT |
QUANTITY |
FRUIT |
|---|---|---|---|
one |
apple |
one |
apple |
输出代码¶
Snowflake¶
Result¶
QUANTITY |
FRUIT |
QUANTITY |
FRUIT |
|---|---|---|---|
one |
apple |
one |
apple |
已知问题 ¶
未发现任何问题。
复合表达式¶
描述 ¶
A compound expression is a series of simple expressions joined by arithmetic operators. A simple expression used in a compound expression must return a numeric value.
(RedShift SQL Language Reference Compound expressions (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html))
语法 ¶
转换表¶
| Redshift | Snowflake | Comments |
|---|---|---|
|| (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (Concatenation) | || | Fully supported by Snowflake |
示例源模式¶
输入代码:¶
Redshift¶
Results¶
concat_string_number |
|---|
Hello has number 42 |
<NULL> |
Redshift has number -7 |
concat_string_date |
|---|
Hello on 2023-12-01 |
<NULL> |
<NULL> |
concat_with_null_handling |
|---|
Hello with number 42 |
Unknown with number 0 |
Redshift with number -7 |
输出代码:
Snowflake¶
Results¶
concat_string_number |
|---|
Hello has number 42 |
<NULL> |
Redshift has number -7 |
concat_string_date |
|---|
Hello on 2023-12-01 |
<NULL> |
<NULL> |
concat_with_null_handling |
|---|
Hello with number 42 |
Unknown with number 0 |
Redshift with number -7 |
已知问题¶
未发现任何问题。
相关的 EWIs¶
没有已知问题。
算术运算符¶
Operators
算术运算符的转换
转换表¶
| Redshift | Snowflake | Comments |
|---|---|---|
| +/- (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (positive and negative sign/operator) | +/- | Fully supported by Snowflake |
| ^ (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (exponentiation) | POWER | Fully supported by Snowflake |
| * (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (multiplication) | * | Fully supported by Snowflake |
| / (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (division) | / | Redshift division between integers always returns integer value, FLOOR function is added to emulate this behavior. |
| % (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (modulo) | % | Fully supported by Snowflake |
| + (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (addition) | + and || | Fully supported by Snowflake. When string are added, it is transformed to a concat. |
| - (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (subtraction) | - | Fully supported by Snowflake |
| @ (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (absolute value) | ABS | Fully supported by Snowflake |
| |/ (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (square root) | SQRT | Fully supported by Snowflake |
| ||/ (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (cube root) | CBRT | Fully supported by Snowflake |
示例源模式¶
加法、减法、正负数¶
输入代码:
输入代码:¶
Redshift¶
Results¶
| positive_value | negative_value | add_sub_result | next_day | one_hour_before | string_sum | int_string_sum | string_int_sum |
|---|---|---|---|---|---|---|---|
| 100.50 | -100.50 | 97.50 | 2024-12-02 10:30:00.000000 | 2024-12-01 09:30:00.000000 | Basic testType A | 105.5 | 105.5 |
| 250.75 | -250.75 | 243.75 | 2024-12-03 15:45:00.000000 | 2024-12-02 14:45:00.000000 | Complex operationsType B | 255.75 | 255.75 |
| -50.25 | 50.25 | -53.25 | 2024-12-04 20:00:00.000000 | 2024-12-03 19:00:00.000000 | Negative base valueType C | -45.25 | -45.25 |
| 0.00 | 0.00 | 8.00 | 2024-12-05 09:15:00.000000 | 2024-12-04 08:15:00.000000 | Zero base valueType D | 5 | 5 |
输出代码:
Snowflake¶
Results¶
| positive_value | negative_value | add_sub_result | next_day | one_hour_before | string_sum | int_string_sum | string_int_sum |
|---|---|---|---|---|---|---|---|
| 100.5 | -100.5 | 97.5 | 2024-12-02 10:30:00 | 2024-12-01 09:30:00 | Basic testType A | 105.5 | 105.5 |
| 250.75 | -250.75 | 243.75 | 2024-12-03 15:45:00 | 2024-12-02 14:45:00 | Complex operationsType B | 255.75 | 255.75 |
| -50.25 | 50.25 | -53.25 | 2024-12-04 20:00:00 | 2024-12-03 19:00:00 | Negative base valueType C | -45.25 | -45.25 |
| 0 | 0 | 8 | 2024-12-05 09:15:00 | 2024-12-04 08:15:00 | Zero base valueType D | 5 | 5 |
指数、乘法、除法和模数¶
输入代码:¶
Redshift¶
Results¶
| raised_to_exponent | multiplied_value | divided_value | int_division | modulo_result | add_sub_result | controlled_eval |
|---|---|---|---|---|---|---|
| 10100.25 | 201 | 20.1 | 20 | 1 | 97.5 | 104.5 |
| 15766047.296875 | 752.25 | 25.075 | 25 | 1 | 243.75 | 259.75 |
| 6375940.62890625 | -251.25 | -6.28125 | -6 | 0 | -53.25 | -30.25 |
| 0 | 0 | 0 | 0 | 1 | 8 | 10 |
输出代码:¶
Snowflake¶
Results¶
| raised_to_exponent | multiplied_value | divided_value | int_division | modulo_result | add_sub_result | controlled_eval |
|---|---|---|---|---|---|---|
| 10100.25 | 201 | 20.1 | 20 | 1 | 97.5 | 104.5 |
| 15766047.2969 | 752.25 | 25.075 | 25 | 1 | 243.75 | 259.75 |
| 6375940.6289 | -251.25 | -6.2812 | -7 | 0 | -53.25 | -30.25 |
| 0 | 0 | 0 | 0 | 1 | 8 | 10 |
绝对值、平方根和立方根¶
输入代码:¶
Redshift¶
Results¶
输出代码:¶
Snowflake¶
Results¶
已知问题¶
在 Snowflake 中,可以对字符串值使用一元运算符
+和-,但在 Redshift 中它无效。
相关的 EWIs¶
无相关的 EWIs。
位运算符¶
Operators
位运算符的转换
转换表¶
| Redshift | Snowflake | Comments |
|---|---|---|
& (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (AND) | BITAND | Fully supported by Snowflake |
| (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (OR) | BITOR | Fully supported by Snowflake |
<< (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (Shift Left) | BITSHIFTLEFT | |
>> (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (Shift Right) | BITSHIFTRIGHT | |
# (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html#r_compound_expressions-arguments) (XOR) | BITXOR | Fully supported by Snowflake |
~ (https://docs.aws.amazon.com/redshift/latest/dg/r_compound_expressions.html) (NOT) | BITNOT | Fully supported by Snowflake |
示例源模式¶
设置数据¶
Redshift¶
Query¶
Snowflake¶
Query¶
整数值的位运算符¶
输入代码:¶
Redshift¶
Results¶
输出代码:
Snowflake¶
Results¶
二进制数据的位运算符¶
对于 BITAND、BITOR 和 BITXOR 函数,如果两个二进制值的长度不同,则会添加 'LEFT' 参数以插入填充,这样做是为了避免在比较 Snowflake 中的值时出错。
Redshift¶
Query¶
Result¶
Snowflake¶
Query¶
Result¶
已知问题¶
未发现任何问题。
相关的 EWIs¶
SSC-FDM-PG0010:由于 Snowflake 的按位函数的行为,结果可能会有所不同。