使用 SQL 获取不同的最大日期
我不太确定如何标题我的问题,但这就是我想做的:
给定 pc_tmppl_tbl
pc_tmppl_attach pc_tmppl_val1 pc_tmppl_crtdt
AJC05-06 AJCINT 2005-08-15 10:32:03.790
AJC06-07 AJCINT 2006-10-17 10:02:06.570
AJC07-08 AJCINT 2007-06-13 10:44:53.573
AJC08-09 AJCINT 2008-06-27 09:51:17.290
AJC09-10 AJCINT 2009-07-20 14:26:06.270
AJC10-11 AJCINT 2010-08-26 11:54:32.777
AJC99-001 AJCINT 2005-05-30 19:30:51.623
ALPI05-06 ALPINE 2005-05-30 19:30:51.623
ALPI07-08 ALPINE 2006-12-11 13:57:09.923
ALPI07-08 ALPINE 2007-05-24 14:04:07.867
ALPI08-09 ALPINE 2008-04-30 09:49:24.140
我希望它返回 pc_tmppl_crtdt 及其相应的 pc_tmppl_attach 的最大日期,所以
ALPI08-09 ALPINE 2008-04-30 09:49:24.140
AJC10-11 AJCINT 2010-08-26 11:54:32.777
我一直在尝试子查询,但还没有非常满意答案,任何帮助将不胜感激。
I'm not sure quite how to title my question but this is what I'm trying to do:
Given pc_tmppl_tbl
pc_tmppl_attach pc_tmppl_val1 pc_tmppl_crtdt
AJC05-06 AJCINT 2005-08-15 10:32:03.790
AJC06-07 AJCINT 2006-10-17 10:02:06.570
AJC07-08 AJCINT 2007-06-13 10:44:53.573
AJC08-09 AJCINT 2008-06-27 09:51:17.290
AJC09-10 AJCINT 2009-07-20 14:26:06.270
AJC10-11 AJCINT 2010-08-26 11:54:32.777
AJC99-001 AJCINT 2005-05-30 19:30:51.623
ALPI05-06 ALPINE 2005-05-30 19:30:51.623
ALPI07-08 ALPINE 2006-12-11 13:57:09.923
ALPI07-08 ALPINE 2007-05-24 14:04:07.867
ALPI08-09 ALPINE 2008-04-30 09:49:24.140
I want it to return the max date for pc_tmppl_crtdt and its corresponding pc_tmppl_attach so
ALPI08-09 ALPINE 2008-04-30 09:49:24.140
AJC10-11 AJCINT 2010-08-26 11:54:32.777
I've been trying sub queries but haven't quite hit on the answer, any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有定义您正在使用的服务器和版本 - 如果您使用的是 SQL Server 2005 或更高版本,您可以使用 CTE(通用表表达式)和排名函数 - 像这样:
基本上,CTE ( inner select) 的作用是从表中获取所有数据,按列
pc_tmppl_val1
对其进行分区 - 因此pc_tmppl_val1
的每组值从 1 开始倒数 - 并对这些条目进行排序按 pc_tmppl_crtdt 降序排列 - 最新条目排在第一位。因此,对于每个
pc_tmppl_val1
值,最新条目是具有RowNumber = 1
的条目,这就是外部SELECT
(基于 CTE)给你。CTE 和排名函数也非常灵活 - 如果您需要为每个
pc_tmppl_val1
值排在前 3 个条目,只需将外部WHERE
条件更改为即可!
CTE(通用表表达式)和排名函数是 ANSI SQL 标准 - 因此除 Microsoft SQL Server 之外的其他数据库也支持它(我只知道 SQL Server 是最好的 - 这就是我使用它作为示例的原因)。
You didn't define what server and version you're using - if you're on SQL Server 2005 or newer, you can use a CTE (Common Table Expression) and a ranking function - something like this:
Basically, what the CTE (inner select) does is grab all data from your table, partition it by your column
pc_tmppl_val1
- so each group of values forpc_tmppl_val1
starts counting back at 1 - and order those entries bypc_tmppl_crtdt
descending - newest entry is first.So for each
pc_tmppl_val1
value, the newest entry is the entry with theRowNumber = 1
and that's what the outerSELECT
(based on the CTE) gives you.The CTE and ranking function are very flexible, too - if you need to top 3 entries for each
pc_tmppl_val1
value, just change the outerWHERE
condition toand you're done!
The CTE (Common Table Expression) and ranking functions are ANSI SQL standard - so other databases besides Microsoft SQL Server support it, too (I just know SQL Server the best - that's why I use it as a sample).
使用相关查询(应该适用于大多数 SQL 数据库):
Using a correlated query (which should work with most sql databases):