我有一个问题:
SELECT
MAX (EXPDATE) AS PREVDATE,
BUSINESSUNIT AS BUSUNIT,
UNIT AS UNITNO
FROM
proddta.V_LeaseExpiry_profile
WHERE
EXPDATE < (
( SELECT
MAX (EXPDATE)
FROM
proddta.V_LeaseExpiry_profile
WHERE
TRIM (BUSINESSUNIT) = '33700' AND UNIT = '900')
)
AND
TRIM (BUSINESSUNIT) = '33700'
AND
UNIT = '900'
GROUP BY
BUSINESSUNIT,
UNIT
当我指定业务单位和单位时,哪个工作正常,但是当我从查询中删除它们时(即 - 我想对所有单位执行此操作),它不再抓住第二个最新日期 - 它抓取最新日期
基本上,我试图找到每个单元的上一个到期日期,但可能有重复的行,所以我需要确保它与MAX ExpDate不同。
答案 0 :(得分:0)
如果您希望第二个到期日期使用dense_rank()
(或offset
/ fetch
):
select p.*
from (select p.*,
dense_rank() over (partition by businessunit, unit
order by EXPDATE desc) as seqnum
from proddta.V_LeaseExpiry_profile
) p
where seqnum = 2;
这将返回所有businessunit / unit组合的适当值。如果只需要一个组合,您可以使用where
子句获得一个组合。