简体   繁体   English

Redshift 不支持 rollup()、grouping() 函数

[英]Redshift does not support rollup(), grouping() functions

Trying to convert Teradata bteq SQL scripts to redshift SQL.尝试将 Teradata bteq SQL 脚本转换为 redshift SQL。 My current redshift Postgres version is 8.0.2, redshift version is 1.0.1499.我当前的红移 Postgres 版本是 8.0.2,红移版本是 1.0.1499。 The current version of redshift does not support rollup(), grouping() functions.当前版本的 redshift 不支持 rollup()、grouping() 函数。 How to overcome and resolve this scenario.如何克服和解决这种情况。 What are the equivalent redshift functions for them?它们的等效红移函数是什么? Could anyone explain with some examples how to do?谁能用一些例子来解释怎么做?

Sample Teradata SQL-示例 Teradata SQL-

select 
PRODUCT_ID,CUST_ID, 
GROUPING (PRODUCT_ID), 
GROUPING (CUST_ID), 
row_number over (order by PRODUCT_ID,CUST_ID) AS "ROW_OUTPUT_NUM"
from products 
group by rollup(PRODUCT_ID,CUST_ID);

Need to convert above sql query to Redshift需要将上面的 sql 查询转换为 Redshift

If you use the UNION technique that others have pointed to, you'll be scanning the underlying table multiple times.如果您使用其他人指出的 UNION 技术,您将多次扫描底层表。

If the fine-level GROUPing actually results in a significant reduction in the data size, a better solution may be:如果精细级别的分组实际上导致数据大小的显着减少,则更好的解决方案可能是:

create temp table summ1 
as
select PRODUCT_ID,CUST_ID, ...
from products 
group by PRODUCT_ID,CUST_ID;

create temp table summ2
as
select PRODUCT_ID,cast(NULL as INT) AS CUST_ID, ...
from products 
group by PRODUCT_ID;

select * from summ1
union all
select * from summ2
union all
select cast(NULL as INT) AS PRODUCT_ID, cast(NULL as INT) AS CUST_ID, ...
from summ2

Implement the ROLLUP by hand手动实现 ROLLUP

Once Redshift does not currently recognize the ROLLUP clause, you must implement this grouping technique in a hard way.一旦 Redshift 当前无法识别 ROLLUP 子句,您就必须以艰难的方式实现这种分组技术。

ROLLUP with 1 argument带 1 个参数的 ROLLUP

With ROLLUP Ex.与 ROLLUP Ex。 PostgreSQL PostgreSQL

SELECT column1, aggregate_function(*)
FROM some_table
GROUP BY ROLLUP(column1)

The equivalent implementation等效实现

-- First, the same GROUP BY without the ROLLUP
-- For efficiency, we will reuse this table
DROP TABLE IF EXISTS tmp_totals;
CREATE TEMP TABLE tmp_totals AS
  SELECT column1, aggregate_function(*) AS total1
  FROM some_table
  GROUP BY column1;

-- Show the table 'tmp_totals'
SELECT * FROM tmp_totals

UNION ALL

-- The aggregation of 'tmp_totals'
SELECT null, aggregate_function(total1) FROM tmp_totals

ORDER BY 1

Example output示例输出

Country  | Sales
-------- | -----
Poland   | 2
Portugal | 4
Ukraine  | 3
null     | 9

ROLLUP with 2 argument带 2 个参数的 ROLLUP

With ROLLUP Ex.与 ROLLUP Ex。 PostgreSQL PostgreSQL

SELECT column1, column2, aggregate_function(*)
FROM some_table
GROUP BY ROLLUP(column1, column2);

The equivalent implementation等效实现

-- First, the same GROUP BY without the ROLLUP
-- For efficiency, we will reuse this table
DROP TABLE IF EXISTS tmp_totals;
CREATE TEMP TABLE tmp_totals AS
  SELECT column1, column2, aggregate_function(*) AS total1
  FROM some_table
  GROUP BY column1, column2;

-- Show the table 'tmp_totals'
SELECT * FROM tmp_totals

UNION ALL

-- The sub-totals of the first category
SELECT column1, null, sum(total1) FROM tmp_totals GROUP BY column1

UNION ALL

-- The full aggregation of 'tmp_totals'
SELECT null, null, sum(total1) FROM tmp_totals

ORDER BY 1, 2;

Example output示例输出

Country  | Segment  | Sales
-------- | -------- | -----
Poland   | Premium  | 0
Poland   | Base     | 2
Poland   | null     | 2     <- sub total
Portugal | Premium  | 1
Portugal | Base     | 3
Portugal | null     | 4     <- sub total
Ukraine  | Premium  | 1
Ukraine  | Base     | 2
Ukraine  | null     | 3     <- sub total
null     | null     | 9     <- grand total

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM