Join新增的列如何创建新列?

How Do I Create a New Column From a Column Newly Added by a Join?

使用左连接,我将来自两个 table 的数据汇集在一起​​。根据我引入的数据,我还使用逻辑语句创建了一个新列(AOIMonth 列)。

我的问题是,如何使用引用新 AOIMonth 列的另一个逻辑语句创建另一个列? AOIMonth 列未在 table 中声明,那么如何引用该列?这是我的代码:

select c.*, b.[FirstMonth], b.[LastMonth], addmonths(b.[lastmonth], c.[Product AOI]) as AOIMonth

from [Churn Custom v4] c
left join (
        select[Line #1], min(Max_Month_Day) as [FirstMonth], max(Max_Month_Day) as 
        [LastMonth]
        from [Churn Custom v4]
        where [Invoiced_Flag]=1
        Group by [Line #1] ) b
    on c.[Line #1] = b.[Line #1]
order by c.[Max_Month_Day]

基本上,我想说,"If AOIMonth = xyz, then abc else 0"谢谢!

您需要重复表达。您不能重复使用别名,除非您使用子查询、CTE 或横向连接。

所以:

(case when addmonths(b.[lastmonth], c.[Product AOI]) = xyz then abc else 0 end) as newcol