以下SQL抛出错误8120,其中说:
列'webgroup_message2_archive.opened'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中
即使我没有使用违规列作为SELECT
子句的一部分。当我更改子查询
SELECT webgroup_message2_testTable.message_id
到一个像(111,222,...等)的列表`它有效!所以问题来自子查询与聚合函数不兼容?我还确保子查询的数据类型与测试表达式匹配。
SELECT
CASE
WHEN arch.opened = '1'
THEN(CASE
WHEN (message_id IN (SELECT webgroup_message2_testTable.message_id
FROM webgroup_message2_testTable))
THEN 'Clicked' ELSE 'Opened' END)
ELSE 'No activity/response'
END AS status, count(1)
FROM webgroup_message2_archive AS arch
INNER JOIN webgroup_development AS dev
ON dev.development_id = arch.HTMLID
WHERE dev.email_tracker_code = 'A4725'
GROUP BY
CASE
WHEN arch.opened= '1'
THEN( CASE
WHEN (message_id IN (SELECT webgroup_message2_testTable.message_id
FROM webgroup_message2_testTable))
THEN 'Clicked' ELSE 'Opened' END)
ELSE 'No activity/response'
END
答案 0 :(得分:2)
我认为您不能在group by
子句中使用子查询。您可以使用left join
查看邮件是否可用来执行相同的操作。
SELECT (CASE WHEN arch.opened= '1'
THEN (CASE WHEN ttmessage_id is not null
THEN 'Clicked'
ELSE 'Opened'
END)
ELSE 'No activity/response'
END), count(1)
FROM webgroup_message2_archive arch INNER JOIN
webgroup_development AS dev
ON dev.development_id = arch.HTMLID left outer join
(select distinct tt.message_id as ttmessage_id
from webgroup_message2_testTable tt
) tt
on message_id = tt.ttmessage_id
WHERE dev.email_tracker_code = 'A4725'
GROUP BY (CASE WHEN arch.opened= '1'
THEN (CASE WHEN ttmessage_id is not null
THEN 'Clicked'
ELSE 'Opened'
END)
ELSE 'No activity/response'
END)
答案 1 :(得分:1)
如果您尝试按状态获取邮件计数,则可以使用左连接并删除子查询。类似于戈登的答案,几分钟击败我,但外观略有不同。这仅在您的意图有效时才有效。
SELECT
CASE
WHEN arch.opened = '1'
THEN (CASE WHEN test.message_id IS NOT NULL THEN 'Clicked' ELSE 'Opened' END)
ELSE 'No activity/response'
END AS status,
count(distinct(arch.message_id))
FROM webgroup_message2_archive AS arch
INNER JOIN webgroup_development AS dev
ON dev.development_id = arch.HTMLID
LEFT JOIN webgroup_message2_testTable AS test
ON arch.message_id = test.message_id
WHERE dev.email_tracker_code = 'A4725'
GROUP BY
CASE
WHEN arch.opened = '1'
THEN (CASE WHEN test.message_id IS NOT NULL THEN 'Clicked' ELSE 'Opened' END)
ELSE 'No activity/response'
END