0

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

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

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]

基本上,我想说,“如果 AOIMonth = xyz,那么 abc else 0” 谢谢!

4

1 回答 1

1

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

所以:

(case when addmonths(b.[lastmonth], c.[Product AOI]) = xyz then abc else 0 end) as newcol
于 2020-02-04T16:50:24.453 回答