微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何限制为 1 年期

如何解决如何限制为 1 年期

查询返回 3 年的记录。如何更改它以返回(最近)1 年的记录?

select id,format(ual.inserted,'yyyy-MM-dd') as inserted
from ual
where id = 347877
group by id,'yyyy-MM-dd')
having format(ual.inserted,'yyyy-MM-dd') >= dateadd(year,-1,max(inserted))
order by format(ual.inserted,'yyyy-MM-dd') desc

我想要做的是为每个 id 返回最近 1 年的记录。

编辑@Squirrel 发表评论后做出的更改:

select id,'yyyy-MM-dd') as inserted
from ual
where webid = 347877 and format(ual.inserted,'yyyy-MM-dd') desc

查询产生此错误

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list,and the column being aggregated is an outer reference.

解决方法

使用子查询找到max的{​​{1}},然后在inserted中使用

where

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。