“Presto SQL 转换为包含子查询的 Hive SQL 时出错”

huangapple go评论97阅读模式
英文:

Error while converting Presto SQL to Hive SQL containing subquery

问题

我试图将以下Presto SQL转换为Hive SQL。在Hive中,WHERE子查询不起作用,并且引发错误。

  1. select distinct id
  2. from account
  3. where load_date = (select max(load_date) from account)
  4. and account_status='current'
  5. and account_type='crnt'

请指导我如何修复这个问题,使其在Hive中工作。

英文:

I am trying to convert the following Presto SQL to Hive SQL. The sub query in WHERE clause isn't working in Hive and is throwing an error.

  1. select distinct id
  2. from account
  3. where load_date = (select max(load_date) from account)
  4. and account_status='current'
  5. and account_type='crnt'

Please advise how I can fix this and make it work in Hive.

答案1

得分: 2

Hive不支持在WHERE子句中使用子查询。可以尝试以下两种方式:

第一种方式:

  1. SELECT DISTINCT a.id
  2. FROM account a
  3. JOIN (
  4. SELECT max(load_date) AS max_load_date
  5. FROM account
  6. ) b
  7. ON a.load_date = b.max_load_date
  8. WHERE a.account_status='current'
  9. AND a.account_type='crnt';

第二种方式:

  1. WITH max_load_date AS (
  2. SELECT max(load_date) AS max_date
  3. FROM account
  4. )
  5. SELECT DISTINCT a.id
  6. FROM account a
  7. JOIN max_load_date m
  8. ON a.load_date = m.max_date
  9. WHERE a.account_status='current'
  10. AND a.account_type='crnt';
英文:

Hive doesn't support subqueries in the WHERE clause. Try this

  1. SELECT DISTINCT a.id
  2. FROM account a
  3. JOIN (
  4. SELECT max(load_date) AS max_load_date
  5. FROM account
  6. ) b
  7. ON a.load_date = b.max_load_date
  8. WHERE a.account_status='current'
  9. AND a.account_type='crnt';

or this

  1. WITH max_load_date AS (
  2. SELECT max(load_date) AS max_date
  3. FROM account
  4. )
  5. SELECT DISTINCT a.id
  6. FROM account a
  7. JOIN max_load_date m
  8. ON a.load_date = m.max_date
  9. WHERE a.account_status='current'
  10. AND a.account_type='crnt';

huangapple
  • 本文由 发表于 2023年5月10日 22:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定