您可以在CASE WHEN THEN别名下进行GROUP BY吗?

您可以在CASE WHEN THEN别名下进行GROUP BY吗?,第1张

您可以在CASE WHEN THEN别名下进行GROUP BY吗?

如果您没有将整个

CASE
语句
GROUP BY
包装在子查询中,则需要使用该语句中的整个语句。

SELECt  CASE  WHEN DATEDIFF(o.EndDate, o.StartDate) < 30 THEN '<1 Month' WHEN DATEDIFF(o.EndDate, o.StartDate) < 90 THEN '1 - 2 Months' WHEN DATEDIFF(o.EndDate, o.StartDate) < 210 THEN '3 - 4 Months' ELSE '>4 Months'         END AS `Length`,         COUNT(DISTINCT(person.ID)) AS `COUNT`FROM    person        INNER JOIN opportunity AS o ON person.EntityID = o.id        INNER JOIN Organization AS org ON o.OrganizationID = Org.IDWHERe   person.TitleID = 2        AND o.bID = 1GROUP   BY  CASE      WHEN DATEDIFF(o.EndDate, o.StartDate) < 30 THEN '<1 Month'     WHEN DATEDIFF(o.EndDate, o.StartDate) < 90 THEN '1 - 2 Months'     WHEN DATEDIFF(o.EndDate, o.StartDate) < 210 THEN '3 - 4 Months'     ELSE '>4 Months'  ENDORDER   BY Length ASC;

还要删除

ORDER BY
子句中列名周围的单引号。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5101538.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存