Hive:不支持的子查询表达式 'extract_date':仅允许作为顶级连词的子查询表达式
Hive: Unsupported SubQuery Expression 'extract_date': Only SubQuery expressions that are top level conjuncts are allowed
我是 HIVE 的新手,我目前在 WSL2 上使用 HIVE 3.1.2,我正在尝试使用命令 在 HIVE CLI 上 运行 这个 SQL 脚本配置单元-f test2subquery.sql:
use mydb;
SELECT
date_photo as extract_date, camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
extract_date as date_photo, camping_code, count(*) as nb_nuit_fid
FROM
extract_resa
WHERE
extract_date = (select CAST(y.extract_date as date) from app_params y)
AND extract_date>= datein
AND extract_date< date_add(datein, nb_nuit_fid)
AND nb_nuit_fid > 0
AND statres <> 'X'
AND id_stay_to = 'NULL' -- eliminer les TO
AND idstay <> idstay_res -- seulement les residents reel
AND client_id_grp <> -1
GROUP BY
extract_date , camping_code
)e
GROUP BY
date_photo, camping_code
但我不断收到此错误:不支持的子查询表达式'extract_date':仅允许作为顶级连词的子查询表达式
我尝试在子查询中使用别名,但我仍然遇到相同的错误。
use mydb;
SELECT
s.extract_date, s.camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
r.extract_date, r.camping_code, count(*) as nb_nuit_fid
FROM
extract_resa r
WHERE
r.extract_date = (select CAST(y.extract_date as date) from app_params y)
AND r.extract_date>= r.datein
AND r.extract_date< date_add(r.datein, nb_nuit_fid)
AND nb_nuit_fid > 0
AND r.statres <> 'X'
AND r.id_stay_to = 'NULL' -- eliminer les TO
AND r.idstay <> r.idstay_res -- seulement les residents reel
AND r.client_id_grp <> -1
GROUP BY
r.extract_date , r.camping_code
)s
GROUP BY
date_photo, camping_code
同样让我感到困惑的是,在评论行 extract_date< date_add(datein, nb_nuit_fid) 时,像这样:
use mydb;
SELECT
s.date_photo as extract_date, s.camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
extract_date as date_photo, camping_code, count(*) as nb_nuit_fid
FROM
extract_resa
WHERE
extract_date < (select cast(y.extract_date as date) from app_params y)
AND
extract_date >= datein
/* AND extract_date < date_add(datein, nb_nuit_fid) */
AND nb_nuit_fid > 0
AND statres <> 'X'
AND id_stay_to = 'NULL' -- eliminer les TO
AND idstay <> idstay_res -- seulement les residents reel
AND client_id_grp <> -1
GROUP BY
extract_date, camping_code
)s
GROUP BY
date_photo, camping_code
脚本运行很顺利。请帮忙
将您的子查询子句转换为内连接。
use mydb;
SELECT
s.extract_date, s.camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
r.extract_date, r.camping_code, count(*) as nb_nuit_fid
FROM
extract_resa r
INNER JOIN (select CAST(y.extract_date as date) ext_dt from app_params y) y ON r.extract_date = y.ext_dt -- this inner join will work same as your filter in subquery
WHERE
r.extract_date>= r.datein
AND r.extract_date< date_add(r.datein, nb_nuit_fid)
AND nb_nuit_fid > 0
AND r.statres <> 'X'
AND r.id_stay_to = 'NULL' -- eliminer les TO
AND r.idstay <> r.idstay_res -- seulement les residents reel
AND r.client_id_grp <> -1
GROUP BY
r.extract_date , r.camping_code
)s
GROUP BY
date_photo, camping_code
我不确定该行的评论效果如何。请告诉我结果。
我是 HIVE 的新手,我目前在 WSL2 上使用 HIVE 3.1.2,我正在尝试使用命令 在 HIVE CLI 上 运行 这个 SQL 脚本配置单元-f test2subquery.sql:
use mydb;
SELECT
date_photo as extract_date, camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
extract_date as date_photo, camping_code, count(*) as nb_nuit_fid
FROM
extract_resa
WHERE
extract_date = (select CAST(y.extract_date as date) from app_params y)
AND extract_date>= datein
AND extract_date< date_add(datein, nb_nuit_fid)
AND nb_nuit_fid > 0
AND statres <> 'X'
AND id_stay_to = 'NULL' -- eliminer les TO
AND idstay <> idstay_res -- seulement les residents reel
AND client_id_grp <> -1
GROUP BY
extract_date , camping_code
)e
GROUP BY
date_photo, camping_code
但我不断收到此错误:不支持的子查询表达式'extract_date':仅允许作为顶级连词的子查询表达式 我尝试在子查询中使用别名,但我仍然遇到相同的错误。
use mydb;
SELECT
s.extract_date, s.camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
r.extract_date, r.camping_code, count(*) as nb_nuit_fid
FROM
extract_resa r
WHERE
r.extract_date = (select CAST(y.extract_date as date) from app_params y)
AND r.extract_date>= r.datein
AND r.extract_date< date_add(r.datein, nb_nuit_fid)
AND nb_nuit_fid > 0
AND r.statres <> 'X'
AND r.id_stay_to = 'NULL' -- eliminer les TO
AND r.idstay <> r.idstay_res -- seulement les residents reel
AND r.client_id_grp <> -1
GROUP BY
r.extract_date , r.camping_code
)s
GROUP BY
date_photo, camping_code
同样让我感到困惑的是,在评论行 extract_date< date_add(datein, nb_nuit_fid) 时,像这样:
use mydb;
SELECT
s.date_photo as extract_date, s.camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
extract_date as date_photo, camping_code, count(*) as nb_nuit_fid
FROM
extract_resa
WHERE
extract_date < (select cast(y.extract_date as date) from app_params y)
AND
extract_date >= datein
/* AND extract_date < date_add(datein, nb_nuit_fid) */
AND nb_nuit_fid > 0
AND statres <> 'X'
AND id_stay_to = 'NULL' -- eliminer les TO
AND idstay <> idstay_res -- seulement les residents reel
AND client_id_grp <> -1
GROUP BY
extract_date, camping_code
)s
GROUP BY
date_photo, camping_code
脚本运行很顺利。请帮忙
将您的子查询子句转换为内连接。
use mydb;
SELECT
s.extract_date, s.camping_code, 0 as nb_credit , 0 as nb_debit, SUM(s.nb_nuit_fid)
FROM
(
SELECT
r.extract_date, r.camping_code, count(*) as nb_nuit_fid
FROM
extract_resa r
INNER JOIN (select CAST(y.extract_date as date) ext_dt from app_params y) y ON r.extract_date = y.ext_dt -- this inner join will work same as your filter in subquery
WHERE
r.extract_date>= r.datein
AND r.extract_date< date_add(r.datein, nb_nuit_fid)
AND nb_nuit_fid > 0
AND r.statres <> 'X'
AND r.id_stay_to = 'NULL' -- eliminer les TO
AND r.idstay <> r.idstay_res -- seulement les residents reel
AND r.client_id_grp <> -1
GROUP BY
r.extract_date , r.camping_code
)s
GROUP BY
date_photo, camping_code
我不确定该行的评论效果如何。请告诉我结果。