如何使此 SQL 查询工作(带有嵌套查询的计算列)?

发布于 2024-10-01 13:26:23 字数 347 浏览 0 评论 0原文

我希望计算列在字段 asofdate 等于表中的最大值 asofdate 时为 true,否则为 false。我尝试了以下操作,但出现语法错误。这样做的正确方法是什么?

select 
case asofdate
when select max(asofdate) from sometable then 1
else 0
end 
from sometable

或者,是否可以有一个计算列

case asofdate
when END OF PREVIOUS MONTH then 1
else 0
end

I want to have a computed column that is true if the field asofdate is equal to the maximum asofdate in the table, otherwise false. I tried the following but I am getting a syntax error. What is the right way to do this?

select 
case asofdate
when select max(asofdate) from sometable then 1
else 0
end 
from sometable

Alternatively, is it possible to have a computed column along the lines of

case asofdate
when END OF PREVIOUS MONTH then 1
else 0
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清欢 2024-10-08 13:26:23

来自 MSDN 文章

计算列是根据
可以使用其他列的表达式
在同一张表中。表达式可以
是非计算列名,
常数、函数和任意
这些通过一个连接的组合
或更多运算符。 表达式不能是子查询。

因此,不能以这种方式创建计算列。

From the MSDN article

A computed column is computed from an
expression that can use other columns
in the same table. The expression can
be a noncomputed column name,
constant, function, and any
combination of these connected by one
or more operators. The expression cannot be a subquery.

So no you can't make a computed column that way.

分开我的手 2024-10-08 13:26:23

您不能在计算列中执行此操作,因为计算列只能根据同一记录中其他列的值进行计算。

您可以在视图中执行此操作:

CREATE VIEW
        v_with_last
AS
SELECT  *, CASE asofdate WHEN MAX(asofdate) OVER () THEN 1 ELSE 0 END AS the_last
FROM    sometable

不幸的是,您无法为此视图建立索引。

You cannot do it in an computed column, since the computed column can be only computed from the values of other columns in the same record.

You can do it in a view, instead:

CREATE VIEW
        v_with_last
AS
SELECT  *, CASE asofdate WHEN MAX(asofdate) OVER () THEN 1 ELSE 0 END AS the_last
FROM    sometable

Unfortunately, you cannot index this view.

怀里藏娇 2024-10-08 13:26:23

在 MS Sql Server 中

SELECT case asofdate 
    WHEN (SELECT MAX(asofdate) FROM sometable) THEN 1 
    ELSE 0 END
FROM sometable

根据评论进行编辑

要获取上个月的月底 -

SELECT DATEADD(dd,-(DAY(asofdate)),asofdate)

In MS Sql Server

SELECT case asofdate 
    WHEN (SELECT MAX(asofdate) FROM sometable) THEN 1 
    ELSE 0 END
FROM sometable

EDIT based on comments

To get the end of the previous month -

SELECT DATEADD(dd,-(DAY(asofdate)),asofdate)
通知家属抬走 2024-10-08 13:26:23

我不知道这是否可行,但您可以创建一个公式为“MAX(asofdate)”的计算列,在该列上创建索引,然后创建一个仅将计算列与 asofdate 进行比较的视图。也许不是最好的解决方案,但可能比仅仅执行子查询稍好一些。

I dont know if this would work, but you could create a computed column with formula as 'MAX(asofdate)', create an index on that column and then create a view that just compares the computed column with asofdate. Maybe not the best possible solution, but might be slightly better than just doing a subquery.

相权↑美人 2024-10-08 13:26:23

所以 asofdate 的最大值存储在一个名为 sometable 的表中,对吧?在这种情况下,您不必对每条记录使用子查询来检查它。您可以将其存储在变量中,然后检查当前的 asofdate。

declare @maxAsOfDate datetime;

Select @maxAsOfDate = max(asofdate) from sometable;

Select case when asofdate >= @maxAsOfDate then 1 else 0 end from sometable

干杯!

so the maximum value of asofdate is stored in a table called sometable right? In that case you dont have to check it using a subquery for every record. You can stored it in a variable and then check the current asofdate towards that.

declare @maxAsOfDate datetime;

Select @maxAsOfDate = max(asofdate) from sometable;

Select case when asofdate >= @maxAsOfDate then 1 else 0 end from sometable

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文