问题描述
此查询返回 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