使用 SQL 获取不同的最大日期

发布于 2024-10-31 23:25:48 字数 893 浏览 0 评论 0原文

我不太确定如何标题我的问题,但这就是我想做的:

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

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

发布评论

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

评论(4

像极了他 2024-11-07 23:25:48
Select T.pc_tmppl_attach, T.pc_tmppl_val1, T.pc_tmppl_crtdt
From pc_temppl_tbl As T
    Join    (
            Select pc_tmppl_val1, Max( T1.pc_tmppl_crtdt ) As MaxDateTime
            From pc_temppl_tbl As T1
            Group By T1.pc_tmppl_val1
            ) As Z
        On Z.pc_tmppl_val1 = T.pc_tmppl_val1
            And Z.MaxDateTime = T.pc_tmppl_crtdt
Select T.pc_tmppl_attach, T.pc_tmppl_val1, T.pc_tmppl_crtdt
From pc_temppl_tbl As T
    Join    (
            Select pc_tmppl_val1, Max( T1.pc_tmppl_crtdt ) As MaxDateTime
            From pc_temppl_tbl As T1
            Group By T1.pc_tmppl_val1
            ) As Z
        On Z.pc_tmppl_val1 = T.pc_tmppl_val1
            And Z.MaxDateTime = T.pc_tmppl_crtdt
哀由 2024-11-07 23:25:48

您没有定义您正在使用的服务器和版本 - 如果您使用的是 SQL Server 2005 或更高版本,您可以使用 CTE(通用表表达式)和排名函数 - 像这样:

;WITH PartitionedData AS
(
   SELECT 
       pc_tmppl_attach, pc_tmppl_val1, pc_tmppl_crtdt,
       ROW_NUMBER() OVER(PARTITION BY pc_tmppl_val1 
                         ORDER BY pc_tmppl_crtdt DESC) AS 'RowNumber'
  FROM dbo.pc_tmppl_tbl
)
SELECT
   pc_tmppl_attach, pc_tmppl_val1, pc_tmppl_crtdt
FROM
   PartitionedData
WHERE
   RowNumber = 1

基本上,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 条件更改为

WHERE RowNumber <= 3

即可!

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:

;WITH PartitionedData AS
(
   SELECT 
       pc_tmppl_attach, pc_tmppl_val1, pc_tmppl_crtdt,
       ROW_NUMBER() OVER(PARTITION BY pc_tmppl_val1 
                         ORDER BY pc_tmppl_crtdt DESC) AS 'RowNumber'
  FROM dbo.pc_tmppl_tbl
)
SELECT
   pc_tmppl_attach, pc_tmppl_val1, pc_tmppl_crtdt
FROM
   PartitionedData
WHERE
   RowNumber = 1

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 for pc_tmppl_val1 starts counting back at 1 - and order those entries by pc_tmppl_crtdt descending - newest entry is first.

So for each pc_tmppl_val1 value, the newest entry is the entry with the RowNumber = 1 and that's what the outer SELECT (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 outer WHERE condition to

WHERE RowNumber <= 3

and 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).

回眸一笑 2024-11-07 23:25:48

使用相关查询(应该适用于大多数 SQL 数据库):

SELECT pc_tmppl_attach,pc_tmppl_val1, pc_tmppl_crtdt
FROM pc_tmppl_tbl AS tbl_ext
WHERE pc_tmppl_crtdt = (
                        SELECT MAX(tbl_int.pc_tmppl_crtdt)
                        FROM pc_tmppl_tbl AS tbl_int
                        WHERE tbl_int.pc_tmppl_val1 = tbl_ext.pc_tmppl_val1
                       )

Using a correlated query (which should work with most sql databases):

SELECT pc_tmppl_attach,pc_tmppl_val1, pc_tmppl_crtdt
FROM pc_tmppl_tbl AS tbl_ext
WHERE pc_tmppl_crtdt = (
                        SELECT MAX(tbl_int.pc_tmppl_crtdt)
                        FROM pc_tmppl_tbl AS tbl_int
                        WHERE tbl_int.pc_tmppl_val1 = tbl_ext.pc_tmppl_val1
                       )
_畞蕅 2024-11-07 23:25:48
SELECT TOP 1 [pc_tmppl_attach], [pc_tmppl_crtdt]
FROM [Given pc_tmppl_tbl]
order by pc_tmppl_crtdt desc
SELECT TOP 1 [pc_tmppl_attach], [pc_tmppl_crtdt]
FROM [Given pc_tmppl_tbl]
order by pc_tmppl_crtdt desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文