如何使用SQL选择最大日期而不使用group by子句。有这样的解决方案有group by子句我想要没有它的解决方案

时间:2015-03-18 07:09:25

标签: sql oracle date group-by

我跟着表:

PERSON_ID   EFFECTIVE_END_DATE      ASSIGNMENT_ID   FULL_NAME
33151       2013-08-04 00:00:00.0   33885           Test, C
33151       2013-10-04 00:00:00.0   33885           Test, C
33151       2015-02-19 00:00:00.0   33885           Test, C
33151       2013-08-04 00:00:00.0   33885           Test, C
33151       2013-10-04 00:00:00.0   33885           Test, C
33151       2015-02-19 00:00:00.0   33885           Test, C

此处PERSON_ID相同我想在不使用group by的情况下选择具有最大有效结束日期的行。

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

在Oracle中,您可以使用:

select * from 
  (SELECT your_column_name 
   FROM table_name 
   order by EFFECTIVE_END_DATE desc) 
where rownum=1;

OR

SELECT *
FROM (
  SELECT your_columns, row_number() over (order by EFFECTIVE_END_DATE desc) EED
      FROM table_name)
WHERE EED = 1

OR

 SELECT *
    FROM table_name as t1
    WHERE EFFECTIVE_END_DATE = (
        SELECT MAX(t2.EFFECTIVE_END_DATE)
        FROM table_name as t2
        WHERE t1.PERSON_ID = t2.PERSON_ID)

参考:

MAX(DATE) - SQL ORACLE

这是一个类似的例子

答案 1 :(得分:1)

您也可能有重复的行。您可以使用分析排名功能来获取所需的行。

设置:

SQL> CREATE TABLE Table1
  2      (PERSON_ID int, EFFECTIVE_END_DATE varchar2(21), ASSIGNMENT_ID int, FULL_NAME varchar2(7));

Table created.

SQL>
SQL> INSERT ALL
  2      INTO Table1 (PERSON_ID, EFFECTIVE_END_DATE, ASSIGNMENT_ID, FULL_NAME)
  3           VALUES (33151, to_date('2013-08-04','YYYY-MM-DD'), 33885, 'Test, C')
  4      INTO Table1 (PERSON_ID, EFFECTIVE_END_DATE, ASSIGNMENT_ID, FULL_NAME)
  5           VALUES (33151, to_date('2013-10-04','YYYY-MM-DD'), 33885, 'Test, C')
  6      INTO Table1 (PERSON_ID, EFFECTIVE_END_DATE, ASSIGNMENT_ID, FULL_NAME)
  7           VALUES (33151, to_date('2015-02-19','YYYY-MM-DD'), 33885, 'Test, C')
  8      INTO Table1 (PERSON_ID, EFFECTIVE_END_DATE, ASSIGNMENT_ID, FULL_NAME)
  9           VALUES (33151, to_date('2013-08-04','YYYY-MM-DD'), 33885, 'Test, C')
 10      INTO Table1 (PERSON_ID, EFFECTIVE_END_DATE, ASSIGNMENT_ID, FULL_NAME)
 11           VALUES (33151, to_date('2013-10-04','YYYY-MM-DD'), 33885, 'Test, C')
 12      INTO Table1 (PERSON_ID, EFFECTIVE_END_DATE, ASSIGNMENT_ID, FULL_NAME)
 13           VALUES (33151, to_date('2015-02-19','YYYY-MM-DD'), 33885, 'Test, C')
 14  SELECT * FROM dual;

6 rows created.

SQL>
SQL> COMMIT;

Commit complete.

SQL>
SQL> SELECT * FROM table1;

 PERSON_ID EFFECTIVE_END_DATE    ASSIGNMENT_ID FULL_NA
---------- --------------------- ------------- -------
     33151 04-AUG-13                     33885 Test, C
     33151 04-OCT-13                     33885 Test, C
     33151 19-FEB-15                     33885 Test, C
     33151 04-AUG-13                     33885 Test, C
     33151 04-OCT-13                     33885 Test, C
     33151 19-FEB-15                     33885 Test, C

6 rows selected.

SQL>

测试案例

SQL> WITH data AS
  2    (SELECT t.*,
  3      rank() over(partition BY person_id order by person_id, EFFECTIVE_END_DATE DESC) rn
  4    FROM table1 t
  5    )
  6  SELECT PERSON_ID,
  7    EFFECTIVE_END_DATE,
  8    ASSIGNMENT_ID,
  9    FULL_NAME
 10  FROM data
 11  WHERE rn = 1;

 PERSON_ID EFFECTIVE_END_DATE    ASSIGNMENT_ID FULL_NA
---------- --------------------- ------------- -------
     33151 19-FEB-15                     33885 Test, C
     33151 19-FEB-15                     33885 Test, C

SQL>

答案 2 :(得分:1)

SELECT *
FROM table t
WHERE NOT EXISTS ( SELECT 'a'
                   FROM table t2
                   WHERE t2.PERSON_ID = t.PERSON_ID
                   AND t2.EFFECTIVE_END_DATE > t.EFFECTIVE_END_DATE
                 )

这是最简单的方式