Sql Server 双子查询
我有一个有点像历史表格的表格...所以我有这样的数据
idA numberMov FinalDate
1 10 20090209
2 14 20090304
1 12 20090304
3 54 20080508
4 42 20090510
... ... ....
我需要根据每个 idA 中的最新 FinalDate 检索 numberMov,因此我使用此
select a.numberMov from (select idA, max(finalDate) ) maxDate from table1 group by idA) as b inner join table1 a on a.idA=b.idA and a.finalDate = b.maxDate
现在我有另一个像这样的查询
select m fields from n table其中 n5.numberMov in ("在此处插入第一个查询")
我觉得有一个更好的解决方案,但想不出任何解决方案,我真的不喜欢在那里有两个子查询。
有什么建议吗?
I have a table which is kinda like an historic table... so I have data like this
idA numberMov FinalDate
1 10 20090209
2 14 20090304
1 12 20090304
3 54 20080508
4 42 20090510
... ... ....
I need to retrieve the numberMov based on the newest finalDate from each idA so I use this
select a.numberMov from (select idA, max(finalDate) maxDate from table1 group by idA) as b inner join table1 a on a.idA=b.idA and a.finalDate = b.maxDate
Now I have another query like this
select m fields from n tables where n5.numberMov in ("insert first query here")
I feel like there is a better solution but can't think of any, I really dont like having two subqueries in there.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有足够的信息来亲自测试它,但类似的东西可能会起作用。
Not enough information to test it myself but something like this might work.
你没有说哪个版本的 SQL Server,但这适用于 SQL 2005+
You don't say which edition of SQL server, but this will work in SQL 2005+