GreenPlum SQL 查询转换成 Hive
Posted
技术标签:
【中文标题】GreenPlum SQL 查询转换成 Hive【英文标题】:GreenPlum SQL query to convert into Hive 【发布时间】:2021-11-08 09:43:34 【问题描述】:下面的查询我们想将greenplum SQL转换为hive sql,请帮助我们。
GP子查询:
select a.region , amount_fr , count_fr , amount_sr , count_sr from (
select region , sum(cast(amount as integer)) as amount_fr , count(transid) as count_fr from test.test_fr_imi
where cast(trans_date as date) between (select cast(add_months(trunc(date_sub(cast(current_date as date),1),'MM'),-1) as date) ) and
(select current_date::date - 1 - substring((select (date_trunc('month',current_date::date ))::date - 1)::character varying,9,10)::integer)
group by 1 ) a
JOIN
(select region , sum(amount::integer) as amount_sr , count(transid) as count_sr from test.test_sr_imi
where trans_date::date between (select (date_trunc('month',current_date::date - 1) - interval '1 month')::date) and
(select current_date::date - 1 - substring((select (date_trunc('month',current_date::date ))::date - 1)::character varying,9,10)::integer) group by 1 ) b on
a.region = b.region;
我需要转换上面的查询,子查询不能转换成hive,请找到上面的特定代码。
(select current_date::date - 1 - substring((select (date_trunc('month',current_date::date))::date - 1)::character 变化的,9,10)::整数)
select to_char(round((select sum(revenue)/1000000.00 from test.sampletable where trxn_date = current_date-1)/1, 2) ,'999,999')
select to_char(round((select sum(case when to_char(trxn_date, 'yyyymm') = to_char((current_date - 1) - '1 month'::interval, 'yyyymm') and extract(day from trxn_date) < extract(day from current_date) then revenue end)/1000000.00 from test.sample2table)/1, 2) ,'999,999')
select to_char(round ((select(( ( sum(case when to_char(trxn_date, 'yyyymm') = to_char((current_date - 1), 'yyyymm') then revenue end)) - (sum(case when to_char(trxn_date, 'yyyymm') = to_char((current_date - 1) - '1 month'::interval, 'yyyymm') and extract(day from trxn_date) < extract(day from current_date) then revenue end) )) / (sum(case when to_char(trxn_date, 'yyyymm') = to_char((current_date - 1) - '1 month'::interval, 'yyyymm') and extract(day from trxn_date) < extract(day from current_date) then revenue end) ))*100.00 from test.sample3table)/1, 2) ,'9990.99%')
【问题讨论】:
【参考方案1】:这里有一些关于某些功能的帮助-
-
round() - 在 hive 中可用。
to_char(INT,'999,999') - 您可以使用
format_number(12345,0)
- 这将产生 12,345
CURRENT_DATE- 在 hive 中可用。
CURRENT_DATE-1 - 这可以使用 current_date - interval '1' day
完成。
hive 中的语法略有不同。您需要将您的 sql 放在子查询中并在其上进行计算。
所以,这里相当于第一个sql-
SELECT format_number(round(sum_rev/1, 2), 0)
FROM
(SELECT sum(revenue)/1000000.00 as sum_rev
FROM test.sampletable
WHERE trxn_date = CURRENT_DATE- interval '1' day) rs
这里是第二个查询的等效 sql
SELECT format_number(round(sum_rev/1, 2), 0)
FROM (SELECT sum(CASE
WHEN from_timestamp(trxn_date, 'yyyyMM') = from_timestamp((CURRENT_DATE - interval '1' day) - interval '1' month, 'yyyyMM')
AND extract(DAY
FROM trxn_date) < extract(DAY
FROM CURRENT_DATE) THEN revenue
END)/1000000.00 as sum_rev
FROM test.sample2table) rs
【讨论】:
(select current_date::date - 1 - substring((select (date_trunc('month',current_date::date))::date - 1)::character varying,9,10): :integer) 我们如何用子查询进行转换 select cast(date_sub(cast(current_date as date),1) - substring((select cast(date_sub(cast(trunc(cast(current_date as date),'MM')as date), 1) 作为字符串),9,10)) 作为 bigint) 我不明白这一点。查询完全改变了。同样,我可以在同一个子查询上看到另一个问题。你能专注于原始查询及其答案吗? @karthikelavan 在有人帮助你并且你说它有效之后,请避免编辑最初的问题。问另一个问题并考虑接受/支持这个问题。当您不断编辑问题时,这是在浪费我们的时间。如果它有效,请不要忘记接受/投票,这就是在这个网站上说“谢谢它有效”的方式。以上是关于GreenPlum SQL 查询转换成 Hive的主要内容,如果未能解决你的问题,请参考以下文章