本文介绍了在 where 子句 SQL 中引用计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行代码是我的 select 语句的一个片段.

This line of code is a snippet from my select statement.

frdFreedays - DateDiff(dd,conReceiptToStock,GetDate()) As FreeDaysRemaining

下面是我的where子句的片段

Below is a snippet from my where clause

and frdFreedays - DateDiff(dd,conReceiptToStock,GetDate()) <= @intFreeDays

我的问题是如何引用 FreeDaysRemaining 列,以便将其与 @intFreeDays

The question I have is how can I reference the FreeDaysRemaining column and so I can compare it to @intFreeDays

我正在寻找这样的东西

Freedays <= @intFreeDays

推荐答案

除了 Aaron 的回答,您还可以使用通用表表达式:

In addition to Aaron's answer, you could use a common table expression:

;with cte_FreeDaysRemaining as
    (
        select
            frdFreedays - DateDiff(dd,conReceiptToStock,GetDate()) As FreeDaysRemaining
            --, more columns
        from yourtable
    )
    select
        FreeDaysRemaining
        --, more columns
    from cte_FreeDaysRemaining
    where FreeDaysRemaining <= @intFreeDays

这篇关于在 where 子句 SQL 中引用计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:39