当我运行以下查询时,联接不会带来联接表字段的数据。似乎 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
但是,如果您使用子查询或为连接表的 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
我真的很难理解这个问题。我还尝试了许多具有不同可能值的 ClickHouse 连接设置,但它根本没有帮助。
感谢您的帮助
我对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