CASE WHEN 判断中以嵌套子查询为条件的 Hive SQL 查询

发布于 2025-01-15 01:49:54 字数 459 浏览 1 评论 0原文

正如标题,我的查询如下:

SELECT
COUNT(
    DISTINCT (CASE WHEN id IN (
        SELECT DISTINCT id
        FROM some_table
        WHERE year = 2019
        AND month IN (1, 2, 3)
    ) tmp AND (*OTHERS CONDITIONS*) THEN id END)
) AS devices
FROM some_table
WHERE year = 2019
AND month IN (1, 2, 3);

但是,我不断收到此错误:

cannot recognize input near 'SELECT' 'DISTINCT' 'id' in expression specification

我该如何解决?

AS the title, my query is like:

SELECT
COUNT(
    DISTINCT (CASE WHEN id IN (
        SELECT DISTINCT id
        FROM some_table
        WHERE year = 2019
        AND month IN (1, 2, 3)
    ) tmp AND (*OTHERS CONDITIONS*) THEN id END)
) AS devices
FROM some_table
WHERE year = 2019
AND month IN (1, 2, 3);

However, I kept receiving this error:

cannot recognize input near 'SELECT' 'DISTINCT' 'id' in expression specification

How can I workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2025-01-22 01:49:54

你不能像这样使用子查询。将子查询更改为 join 并在以防万一时使用它。

SELECT
COUNT(
    DISTINCT (CASE WHEN tmpid is not null 
         AND (*OTHERS CONDITIONS*) THEN id END)
) AS devices
FROM some_table
left join (SELECT DISTINCT id as tmpid
        FROM some_table
        WHERE year = 2019
        AND month IN (1, 2, 3)
    ) tmp on tmp.tmpid = id
WHERE year = 2019
AND month IN (1, 2, 3);

you can not use subquery like this. Change the subquery to join and use it in case when.

SELECT
COUNT(
    DISTINCT (CASE WHEN tmpid is not null 
         AND (*OTHERS CONDITIONS*) THEN id END)
) AS devices
FROM some_table
left join (SELECT DISTINCT id as tmpid
        FROM some_table
        WHERE year = 2019
        AND month IN (1, 2, 3)
    ) tmp on tmp.tmpid = id
WHERE year = 2019
AND month IN (1, 2, 3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文