SQL选择ID相同的多个最大行

时间:2013-06-17 05:29:33

标签: sql

我一次又一次地在这方面挣扎,但无法让它发挥作用。已经在这些论坛上待了几个小时......数据集:

Row Date            AccountID   Column 1    
1   21/02/2013 0:30     A0M8FA1003YP    .       
2   21/02/2013 0:30     A0M8FA1003YP    .       
3   21/02/2013 0:30     A0M8FA1003YP    .       
4   24/09/2007 12:00    A0M8FA1003YP    .       
5   24/09/2007 12:00    A0M8FA1003YP    .       
6   24/09/2007 12:00    A0M8FA1003YP    .       
7   12/02/2009 12:00    A023123332YP    .       
8   24/09/2003 12:00    A023123332YP    .       
9   24/09/2003 12:00    A023123332YP    .       
10  24/09/2003 12:00    A023123332YP    .           

我想返回日期列的最大值,但不仅返回单行,而是返回与该最大值匹配的任何行。即在上面的集合中,我想返回行1,2,3和7(行的所有列)。

Row Date                AccountID       Column 1    
1   21/02/2013 0:30     A0M8FA1003YP    .       
2   21/02/2013 0:30     A0M8FA1003YP    .       
3   21/02/2013 0:30     A0M8FA1003YP    .   
7   12/02/2009 12:00    A023123332YP    .       

我有数千行,每个ACCOUNTID返回的匹配行数会有所不同,有些是1,有些是2,有些是10.请帮帮我!!!

更新 还试过这个

选择max(ASS_SCH_DATE)over(AccountID分区),AccountID,ASS_SCH_DATE,ACCOUNTID#Temp3 order by#Temp3.ACCOUNTID

结果仍然显示额外的行。


(No column name)            ASS_SCH_DATE                ACCOUNTID
2013-02-21 00:30:00.000     2013-02-21 00:30:00.000     A0M8FA1003YP
2013-02-21 00:30:00.000     2013-02-21 00:30:00.000     A0M8FA1003YP
2013-02-21 00:30:00.000     2013-02-21 00:30:00.000     A0M8FA1003YP
2013-02-21 00:30:00.000     2007-09-24 12:00:00.000     A0M8FA1003YP
2013-02-21 00:30:00.000     2007-09-24 12:00:00.000     A0M8FA1003YP

6 个答案:

答案 0 :(得分:5)

查询:

<强> SQLFIDDLEExample

SELECT t1.*
FROM Table1 t1
WHERE t1.Date = (SELECT MAX(t2.Date)
                 FROM Table1 t2
                 WHERE t2.AccountID = t1.AccountID)

结果:

| ROW |                            DATE |    ACCOUNTID |
--------------------------------------------------------
|   1 | February, 21 2013 00:30:00+0000 | A0M8FA1003YP |
|   2 | February, 21 2013 00:30:00+0000 | A0M8FA1003YP |
|   3 | February, 21 2013 00:30:00+0000 | A0M8FA1003YP |
|   7 | February, 12 2009 12:00:00+0000 | A023123332YP |

答案 1 :(得分:1)

select * from table where date in (select max(date) from table)

答案 2 :(得分:0)

您正在寻找Windows功能:

Select row,max(date) over (partition by AccountID) As max_date, AccountID, 
Column1 from table where max_date = date;

答案 3 :(得分:0)

试试这个,

select * from tablename where date in (select max(date) from table group by AccountID)

请参阅sqlFiddle demo。它提供的是您期望的相同输出。

答案 4 :(得分:0)

尝试使用Ranks

SELECT  AccountID, 
        ASS_SCH_DATE, 
        RANK() OVER (PARTITION BY ASS_SCH_DATE ORDER BY AccountID) DateRank
FROM YourTable
WHERE DateRank = 1

这样你也可以选择最高等级,第二等等......

答案 5 :(得分:0)

容易...

在你的桌子上'假',我会写:

SELECT dummy.*
FROM
  (
      SELECT MAX(Date) Date, AccountID
      FROM dummy
      GROUP BY AccountID
  ) max_date
    INNER JOIN dummy
    ON(
          dummy.Date      = max_date.Date
      AND dummy.AccountID = max_date.AccountID
    )

或者,使用更现代,更语义的语法:

WITH max_date(Date, AccountID)
AS
(
  SELECT MAX(Date) Date, AccountID
  FROM dummy
  GROUP BY AccountID
)
SELECT dummy.*
FROM
  max_date
    INNER JOIN dummy
    ON(
          dummy.Date      = max_date.Date
      AND dummy.AccountID = max_date.AccountID
    )