按组从表中获取Max()记录

发布于 2024-08-27 10:34:21 字数 1325 浏览 7 评论 0原文

我有一个这样的表:

Article Number  Last Voucher Number Last Voucher Date
0557934         519048                  04/02/2005
0557934         519067                  04/02/2005
0557934         528630                  09/29/2005
0557934         528631                  09/29/2005
0557934         529374                  10/13/2005
0557934         529375                  10/13/2005
0557934         529471                  10/16/2005
0557934         529472                  10/16/2005
0557934         535306                  01/08/2006
0557934         535307                  01/08/2006
0557934         1106009                 08/10/2006
0557934         1106010                 08/10/2006
0022738         22554                   02/20/1995
0022738         22595                   03/12/1995
0022738         22597                   03/15/1995
0022738         22605                   03/19/1995
0022738         22616                   03/25/1995
0022738         22621                   03/28/1995
0022738         22630                   04/05/1995

我只想有最后日期的记录:

Article Number  Last Voucher Number Last Voucher Date
0557934         1106010                 08/10/2006
0022738         22630                   04/05/1995

我可以直接在 SQL 或 Linq 上执行。

有什么想法吗?

i've got a table like that :

Article Number  Last Voucher Number Last Voucher Date
0557934         519048                  04/02/2005
0557934         519067                  04/02/2005
0557934         528630                  09/29/2005
0557934         528631                  09/29/2005
0557934         529374                  10/13/2005
0557934         529375                  10/13/2005
0557934         529471                  10/16/2005
0557934         529472                  10/16/2005
0557934         535306                  01/08/2006
0557934         535307                  01/08/2006
0557934         1106009                 08/10/2006
0557934         1106010                 08/10/2006
0022738         22554                   02/20/1995
0022738         22595                   03/12/1995
0022738         22597                   03/15/1995
0022738         22605                   03/19/1995
0022738         22616                   03/25/1995
0022738         22621                   03/28/1995
0022738         22630                   04/05/1995

I would like to have only the record with the last date :

Article Number  Last Voucher Number Last Voucher Date
0557934         1106010                 08/10/2006
0022738         22630                   04/05/1995

I can do directly on SQL or on Linq.

Any idea?

Ju

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

野稚 2024-09-03 10:34:21

最简单的方法可能是使用row_number。这也会打破同一凭证日期的联系:

select *
from (
    select 
        *
    ,   row_number() over (partition by [Article Number] 
          order by [Last Voucher Date] desc, [Last Voucher Number] desc) as RowNr
    from YourTable
) as SubQueryAlias
where RowNr = 1

另一个选项是 where not contains 子句:

select *
from YourTable yt
where not exists (
    select *
    from YourTable yt2
    where yt.[Article Number] = yt2.[Article Number]
    and yt.[Last Voucher Date] < yt2.[Last Voucher Date]
)

如果最新凭证日期存在联系,这将返回两行。

The simplest way is probably using row_number. This also breaks ties for the same voucher date:

select *
from (
    select 
        *
    ,   row_number() over (partition by [Article Number] 
          order by [Last Voucher Date] desc, [Last Voucher Number] desc) as RowNr
    from YourTable
) as SubQueryAlias
where RowNr = 1

Another option is a where not exists clause:

select *
from YourTable yt
where not exists (
    select *
    from YourTable yt2
    where yt.[Article Number] = yt2.[Article Number]
    and yt.[Last Voucher Date] < yt2.[Last Voucher Date]
)

In case of ties for the latest voucher date, this would return both rows.

混浊又暗下来 2024-09-03 10:34:21

您需要提取每篇文章的最后日期,然后返回以获取最后的优惠券编号。否则,您也将根据最后的优惠券编号进行分组。

当然,这会返回文章 0557934 的 2 行,但同一日期的平局决胜标准是什么?
我发现这比使用 ROW_NUMBER 的表现更好,但是 YMMV.And ROW_NUMBER 是您处理抢七情况的方法

SELECT
    M.*
FROM
    (
    SELECT
        MAX([Last Voucher Date]) AS LastDate,
        [Article Number]
    FROM
        Mytable
    GROUP BY
        [Article Number]
    ) MMax
    JOIN MyTable M ON MMax.[Article Number] = M.[Article Number] AND MMax.LastDate = M.[Last Voucher Date]

You need to extract the the last date per article, then join back to pick up Last Voucher Number. Otherwise, you'll group on Last Voucher Number too.

Of course, this returns 2 rows for article 0557934 but what is the tie break criteria for the same date?
This performs better more often that using ROW_NUMBER I've found, but YMMV.And ROW_NUMBER is how you could deal with the tiebreak situation

SELECT
    M.*
FROM
    (
    SELECT
        MAX([Last Voucher Date]) AS LastDate,
        [Article Number]
    FROM
        Mytable
    GROUP BY
        [Article Number]
    ) MMax
    JOIN MyTable M ON MMax.[Article Number] = M.[Article Number] AND MMax.LastDate = M.[Last Voucher Date]
指尖上的星空 2024-09-03 10:34:21

如果您使用 Oracle,您将执行以下操作:

select *
from 
  (select *
  from Table
  order by voucher_date)
where rownum = 1

If you're using Oracle, you would do something like this:

select *
from 
  (select *
  from Table
  order by voucher_date)
where rownum = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文