简体   繁体   English

选择最大日期,然后在WHERE子句中使用它

[英]Selecting Max Date, then using it in WHERE clause

I'm sure there is an easy solution to this, but I am wracking my brain here trying to figure it out. 我敢肯定有一个简单的解决方案,但是我在这里想弄清楚我的大脑。 Sometimes an extra set of eyes can save the day! 有时额外的眼睛可以拯救一天!

SELECT S.StoreId, MAX(AL.ActivityDate) AS LastActivity FROM Stores S
INNER JOIN dbo.ActivityLog AL ON AL.StoreID = S.StoreID
WHERE S.StoreID IN (SELECT StoreID FROM Stores WHERE Status = 2)
AND AL.ActivityDate < DATEADD(DAY, -180, GETDATE())
GROUP BY S.StoreID
ORDER BY LastActivity

My goal: Pull all the stores that have a MAX Activity Date older than 6 months (180 days). 我的目标:拉出所有MAX活动日期超过6个月(180天)的商店。 This bit of code doesn't seem to be doing the trick... Any ideas? 这段代码似乎并没有解决问题……有什么想法吗?

Thanks! 谢谢!

I think you can do what you want with group by and having : 我认为你可以做你想做什么group byhaving

select al.StoreId, MAX(al.ActivityDate) AS LastActivity
from dbo.ActivityLog al
group by al.StoreId
having MAX(al.ActivityDate) < DATEADD(day, -100, getdate());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM