Clickhouse Join with where 子句无法按预期工作

问题描述 投票:0回答:1

当我运行以下查询时,联接不会带来联接表字段的数据。似乎 where 子句中的最后一项 ('AND ssp_reporting__account=21') 导致了此问题。

SELECT
  `s_s_p_reporting_stats`.account_id `ssp_reporting__account`,
  `s_s_p_accounts`.id,
  `s_s_p_accounts`.name
FROM adplatform_dev.ssp_dist AS `s_s_p_reporting_stats`
LEFT JOIN adplatform_dev_ssp_facts.accounts AS `s_s_p_accounts` 
ON toInt64(`s_s_p_accounts`.id) = toInt64(`s_s_p_reporting_stats`.account_id)
WHERE `s_s_p_reporting_stats`.time >= parseDateTimeBestEffort('2025-01-01T00:00:00.000Z') 
AND `s_s_p_reporting_stats`.time <= parseDateTimeBestEffort('2025-01-13T23:59:59.000Z') 
AND ssp_reporting__account=21 
LIMIT 50

The data when you add where clause inside the query

但是,如果您使用子查询或为连接表的 ID 添加 or 条件来查询它,它就可以正常工作。

SELECT
  `s_s_p_reporting_stats`.account_id `ssp_reporting__account`,
  `s_s_p_accounts`.id,
  `s_s_p_accounts`.name
FROM adplatform_dev.ssp_dist AS `s_s_p_reporting_stats`
LEFT JOIN adplatform_dev_ssp_facts.accounts AS `s_s_p_accounts` 
ON toInt64(`s_s_p_accounts`.id) = toInt64(`s_s_p_reporting_stats`.account_id)
WHERE `s_s_p_reporting_stats`.time >= parseDateTimeBestEffort('2025-01-01T00:00:00.000Z') 
AND `s_s_p_reporting_stats`.time <= parseDateTimeBestEffort('2025-01-13T23:59:59.000Z') 
AND (ssp_reporting__account=21 OR `s_s_p_accounts`.id = 21)
LIMIT 50

SELECT * FROM (SELECT
  `s_s_p_reporting_stats`.account_id `ssp_reporting__account`,
  `s_s_p_accounts`.id,
  `s_s_p_accounts`.name
FROM adplatform_dev.ssp_dist AS `s_s_p_reporting_stats`
LEFT JOIN adplatform_dev_ssp_facts.accounts AS `s_s_p_accounts` 
ON toInt64(`s_s_p_accounts`.id) = toInt64(`s_s_p_reporting_stats`.account_id)
WHERE `s_s_p_reporting_stats`.time >= parseDateTimeBestEffort('2025-01-01T00:00:00.000Z') 
AND `s_s_p_reporting_stats`.time <= parseDateTimeBestEffort('2025-01-13T23:59:59.000Z') 
LIMIT 50) WHERE ssp_reporting__account = 21

The data when you add where clause inside the query

我真的很难理解这个问题。我还尝试了许多具有不同可能值的 ClickHouse 连接设置,但它根本没有帮助。

感谢您的帮助

sql clickhouse
1个回答
0
投票

我对Clickhouse了解不多,但一般在sql中别名不能在WHERE子句中直接引用。所以我改变了你最后的条件。请尝试以下。

SELECT
`s_s_p_reporting_stats`.account_id AS `ssp_reporting__account`,
`s_s_p_accounts`.id,
`s_s_p_accounts`.name
FROM adplatform_dev.ssp_dist AS `s_s_p_reporting_stats`
LEFT JOIN adplatform_dev_ssp_facts.accounts AS `s_s_p_accounts` 
ON toInt64(`s_s_p_accounts`.id) = toInt64(`s_s_p_reporting_stats`.account_id)
WHERE `s_s_p_reporting_stats`.time >= parseDateTimeBestEffort('2025-01-01T00:00:00.000Z') 
AND `s_s_p_reporting_stats`.time <= parseDateTimeBestEffort('2025-01-13T23:59:59.000Z') 
AND `s_s_p_reporting_stats`.account_id = 21
LIMIT 50
© www.soinside.com 2019 - 2024. All rights reserved.