按组从表中获取Max()记录
我有一个这样的表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法可能是使用
row_number
。这也会打破同一凭证日期的联系:另一个选项是
where not contains
子句:如果最新凭证日期存在联系,这将返回两行。
The simplest way is probably using
row_number
. This also breaks ties for the same voucher date:Another option is a
where not exists
clause:In case of ties for the latest voucher date, this would return both rows.
您需要提取每篇文章的最后日期,然后返回以获取最后的优惠券编号。否则,您也将根据最后的优惠券编号进行分组。
当然,这会返回文章 0557934 的 2 行,但同一日期的平局决胜标准是什么?
我发现这比使用 ROW_NUMBER 的表现更好,但是 YMMV.And ROW_NUMBER 是您处理抢七情况的方法
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
如果您使用 Oracle,您将执行以下操作:
If you're using Oracle, you would do something like this: