gpt4 book ai didi

选择最近日期的 SQL 语句不再有效

转载 作者:行者123 更新时间:2023-12-04 21:24:08 24 4
gpt4 key购买 nike

我的一个报告例程突然失败,并将其追溯到我声明的 having 部分。直到 2 天前,它一直在使用的功能是从 dbo.data_feed_file 表(列名:File_Date)中选择最近的日期。

声明如下

HAVING (dbo.data_feed_file.file_date = (Select MAX(File_Date) as Expr1
FROM dbo.data_feed_file AS data_feed_file_1))

首先:有没有其他的写法?我已经通过删除声明使我的报告正常工作,它只比我想要的多了 250 万行。我知道我可以对日期进行硬编码以提取我想要的特定日期,但自动化显然是首选。

第二:有谁知道什么会导致它自发失败?我是唯一有权编辑此查询的人,所以我知道什么都没有改变(真的,什么都没有改变)。

提前致谢。

编辑:添加说明:没有错误消息,列标题按预期显示但没有填充数据,它只是空白字段(好像没有任何内容符合 having 标准)。语句完成,就好像没有任何问题一样。我已确认 File_Date 列中没有 NULL 值。

最佳答案

我能想到没有行返回的两个原因。第一个是子查询返回 NULL。这很容易修复为:

HAVING (dbo.data_feed_file.file_date = (Select MAX(File_Date) as Expr1
FROM dbo.data_feed_file AS data_feed_file_1
where file_date is not null))

第二个是 File_Date 存储为 datetime,而不是 date。如果是这样,您可能有一个过滤掉最新值的 where 子句,并且在 having 子句中遗漏了它。如果您想要日期,但该值存储为日期时间,那么您可以尝试:

HAVING (cast(dbo.data_feed_file.file_date as date) =
(Select cast(MAX(File_Date) as date) as Expr1
FROM dbo.data_feed_file AS data_feed_file_1
where file_date is not null))

关于选择最近日期的 SQL 语句不再有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com