我正在尝试创建 2 个临时表的联合,但查询仅从第一个表返回值。
通过搜索我唯一能找到的是你不能在查询中多次引用同一个临时表,但我没有这样做。两个临时表都基于同一个源表,但它们是单独的临时表,单独运行时两者都运行良好:
编辑 - 更多详细信息:来自 enrols_new 的记录没有出现在联合中,但是如果我将 enrols_new 与另一个从不同源表派生的临时表合并,它就可以正常工作。所以看起来确实与基于相同源表的两个 temp_tables 有关
DROP TEMPORARY TABLE IF EXISTS apps_new;
CREATE TEMPORARY TABLE apps_new AS
SELECT bi_data.t_sugar_apps.ClientID, 'Application' AS `type`,
MIN(bi_data.t_sugar_apps.date_created) AS date_created,
bi_data.t_sugar_apps.adm_score, bi_data.t_sugar_apps.delivery,
'Application' AS lead_source
FROM bi_data.t_sugar_apps
INNER JOIN arden_ga_job.arden_topline
ON bi_data.t_sugar_apps.ClientID = arden_ga_job.arden_topline.ClientID
WHERE bi_data.t_sugar_apps.region = 'UK' AND
arden_ga_job.arden_topline.RowDate <= bi_data.t_sugar_apps.date_created
GROUP BY bi_data.t_sugar_apps.ClientID, `type`,
bi_data.t_sugar_apps.adm_score,
bi_data.t_sugar_apps.delivery,
bi_data.t_sugar_apps.lead_source;
DROP TEMPORARY TABLE IF EXISTS enrols_new;
CREATE TEMPORARY TABLE enrols_new AS
SELECT bi_data.t_sugar_apps.ClientID, 'Enrol' AS `type`,
MIN(bi_data.t_sugar_apps.date_enrolled) AS date_created,
NULL AS adm_score, bi_data.t_sugar_apps.delivery,
'Enrol' AS lead_source
FROM bi_data.t_sugar_apps
INNER JOIN arden_ga_job.arden_topline
ON bi_data.t_sugar_apps.ClientID = arden_ga_job.arden_topline.ClientID
WHERE bi_data.t_sugar_apps.date_enrolled IS NOT NULL AND bi_data.t_sugar_apps.region = 'UK'
AND arden_ga_job.arden_topline.RowDate <= bi_data.t_sugar_apps.date_created
GROUP BY bi_data.t_sugar_apps.ClientID, `type`, bi_data.t_sugar_apps.adm_score, bi_data.t_sugar_apps.delivery,
bi_data.t_sugar_apps.lead_source;
DROP TEMPORARY TABLE IF EXISTS clients;
CREATE TEMPORARY TABLE clients AS
SELECT ClientID, `type`, date_created, adm_score, delivery, lead_source
FROM (
SELECT ClientID, `type`, date_created, adm_score, delivery, lead_source
FROM apps_new
UNION ALL
SELECT ClientID, `type`, date_created, adm_score, delivery, lead_source
FROM enrols_new
) AS uni
GROUP BY ClientID;
计划A:摆脱
GROUP BY ClientID
B 计划:意识到
GROUP BY
是病式的。当每个 ClientID 请求一行时,哪一行?应用程序行?或注册行?计划 C:使用某种形式的聚合,例如
GROUP_CONCAT(type)
用于各种不明确的列。