如何找到列中相关日期之间的最大范围?

How to find the largest range between related dates in a column?

我有一个数据库,其中一列有乐队,另一列有他们专辑的发行日期,我如何根据发行日期之间的差异找到最老的乐队? IE。最老的乐队不是 Band1,因为他们在 1968 年首次发行专辑,按照我的方法,Band1 只有 7 岁(或者活跃 7 年可能更好),而 Band2 是 26 岁(活跃年),因为 2001 - 1975 = 26.

例如:

Band......Rel_date

Band1....12/12/68

Band1....08/05/75

Band2....09/02/75

Band2....05/05/99

Band2....03/05/01

如果我明白你在问什么

SELECT BAND
  FROM YOUR_TABLE
  WHERE REL_DATE = (SELECT MIN(REL_DATE)
                      FROM YOUR_TABLE)

应该这样做。

编辑

根据进一步的信息

WITH cteBand_activity AS
        (SELECT BAND,
                MIN(RELEASE_DATE) AS FIRST_RELEASE_DATE,
                MAX(RELEASE_DATE) AS LATEST_RELEASE_DATE,
                TRUNC((MAX(RELEASE_DATE) - MIN(RELEASE_DATE)) / 365) + 1 AS YEARS_ACTIVE
           FROM ALBUMS
           GROUP BY BAND
           ORDER BY YEARS_ACTIVE DESC)
SELECT BAND, YEARS_ACTIVE
  FROM cteBand_activity
  WHERE ROWNUM = 1

应该会给出您要查找的结果。

dbfiddle here

祝你好运。

甲骨文 12c+

select band from your_table order by  rel_date fetch first 1 rows only; 

如果您想要包含平局,请将其更改为 with ties 而不是 only

编辑

select   band  from t 
group by band order by max(rel_date) - min(rel_date) desc fetch first 1 rows only;

EDIT2

对于 12c 之前的 Oracle 版本:

select * from
(
    select   band  from t 
    group by band order by max(rel_date) - min(rel_date) desc
) where rownum = 1