You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Amazon Redshift中关联两个WITH临时表报错,求正确SQL写法

解决多个WITH临时表关联的语法错误问题

你碰到的这个语法错误,核心原因是定义多个CTE(WITH临时表)时,不需要重复使用WITH关键字——第一个CTE用WITH开头,后续的CTE直接用逗号分隔即可。

你的原代码里连续写了两个WITH,这不符合SQL的语法规范,所以触发了语法错误。下面是修正后的正确写法:

WITH temp AS (
    SELECT 
        account_id, 
        asm_signatures_classification, 
        count(*) AS classification_count  -- 给统计结果加别名,让输出列更清晰
    FROM asm_insights 
    WHERE date = '2020-05-20' 
    GROUP BY account_id, asm_signatures_classification 
    ORDER BY account_id
), temp2 AS (
    SELECT 
        account_id, 
        app_id 
    FROM asm_insights 
    WHERE date = '2020-05-20'
)
SELECT * 
FROM temp 
JOIN temp2 ON temp.account_id = temp2.account_id;

额外提个小建议:给count(*)加上别名(比如classification_count),这样查询结果里的统计列会更直观,避免默认列名带来的混淆。

内容的提问来源于stack exchange,提问作者meitale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:17:45