0

我是 SQL 新手,这是我的 uni 项目。我有一个带有列的表 Rego, FirstRego, LastRego, RegoDue

假设我有以下数据:

  Rego      FirstRego                      LastRego                      RegoDue
YGF 615 2011-04-07 00:00:00.000 2011-04-07 00:00:00.000 2012-04-07 00:00:00.000
YGF 615 2011-04-07 00:00:00.000 2012-04-07 00:00:00.000 2013-04-07 00:00:00.000
ZIR 377 2012-10-05 00:00:00.000 2012-10-05 00:00:00.000 2013-10-05 00:00:00.000
ZIR 377 2012-10-05 00:00:00.000 2013-10-05 00:00:00.000 2014-10-05 00:00:00.000
ZJT 795 2012-10-31 00:00:00.000 2012-10-31 00:00:00.000 2013-10-31 00:00:00.000
ZSU 823 2012-04-30 00:00:00.000 2012-04-30 00:00:00.000 2013-04-30 00:00:00.000

在查询输出中,我希望它Rego是最高的,RegoDue即输出应该如下所示:

Rego      FirstRego                      LastRego                      RegoDue
YGF 615 2011-04-07 00:00:00.000 2012-04-07 00:00:00.000 2013-04-07 00:00:00.000
ZIR 377 2012-10-05 00:00:00.000 2013-10-05 00:00:00.000 2014-10-05 00:00:00.000
ZJT 795 2012-10-31 00:00:00.000 2012-10-31 00:00:00.000 2013-10-31 00:00:00.000
ZSU 823 2012-04-30 00:00:00.000 2012-04-30 00:00:00.000 2013-04-30 00:00:00.000

我怎样才能做到这一点?

4

2 回答 2

0

首先,您要查找最新的 rego 到期日。一旦你有了这个,你就可以加入它来过滤掉不需要的结果。

select
    t.Rego, t.FirstRego, t.LastRego, lr.LatestRegoDue as RegoDue
from Table1 t
    join
    (
        select
           Rego, max(RegoDue) as LatestRegoDue
        from Table1
        group by Rego
    ) lr on t.Rego = lr.Rego and t.RegoDue = lr.LatestRegoDue
于 2013-10-21T01:35:45.120 回答
0
Select * 
From Tbl T1
Where RegoDue =
(
    Select Max(RegoDue)
    From Tbl T2
    Where T2.Rego = T1.Rego
)
于 2013-10-20T12:19:56.030 回答