Sql Server 双子查询

发布于 2024-08-06 21:03:49 字数 672 浏览 1 评论 0原文

我有一个有点像历史表格的表格...所以我有这样的数据

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-08-13 21:03:49

没有足够的信息来亲自测试它,但类似的东西可能会起作用。

select m fields
from a inner join
 (select numberMov,
  max(FinalDate) as maxDate
 from a
 group by numberMov) b 
  on a.numberMov = b.numberMov
  and a.FinalDate = b.maxDate inner join
 n tables on a.numberMov = n.numberMov

Not enough information to test it myself but something like this might work.

select m fields
from a inner join
 (select numberMov,
  max(FinalDate) as maxDate
 from a
 group by numberMov) b 
  on a.numberMov = b.numberMov
  and a.FinalDate = b.maxDate inner join
 n tables on a.numberMov = n.numberMov
゛时过境迁 2024-08-13 21:03:49

你没有说哪个版本的 SQL Server,但这适用于 SQL 2005+

;WITH rankCTE
AS
(
    SELECT idA
           ,numberMov
           ,FinalDate
           ,ROW_NUMBER() OVER (PARTITION BY idA
                               ORDER BY FinalDate DESC
                              ) AS rn
    FROM table1
)
,latestCTE
AS 
(
    SELECT idA
           ,numberMov
           ,FinalDate
    FROM rankCTE
    WHERE rn = 1
)
SELECT m fields 
FROM n tables 
WHERE n5.numberMov IN (SELECT numberMov FROM latestCTE)

You don't say which edition of SQL server, but this will work in SQL 2005+

;WITH rankCTE
AS
(
    SELECT idA
           ,numberMov
           ,FinalDate
           ,ROW_NUMBER() OVER (PARTITION BY idA
                               ORDER BY FinalDate DESC
                              ) AS rn
    FROM table1
)
,latestCTE
AS 
(
    SELECT idA
           ,numberMov
           ,FinalDate
    FROM rankCTE
    WHERE rn = 1
)
SELECT m fields 
FROM n tables 
WHERE n5.numberMov IN (SELECT numberMov FROM latestCTE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文