我如何在 sql 中的 case 语句别名上使用 group by 子句?

How can i use group by clause on case statement aliasing in tsql?

我的查询中有一个带有别名的 case 语句,我试图用 group-by 子句显示它,但我一直收到错误“无效的列名 (case 语句别名)"

代码

Select column 1,
       column 2,
       column 3,
       Case When .... then ...
            When .... then ...
            When .... then ... 
            else ... as X
From table1,
     table 2,
     table 3
where condition 1,
      condition 2,
      condition 3
group by column 1,
         column 2,
         X,
         column 3;

输出:列名无效 'X'。

您需要在 group by 子句中包含整个 case 语句,而不是使用别名。所以

group by column 1,
         column 2,
       Case When .... then ...
            When .... then ...
            When .... then ... 
            else ... ,
         column 3;