如何在 sql 中的 table 中获取最多一个日期
How to get max of one date in a table in sql
我写了下面的代码-
select
PAPF.PERSON_NUMBER,
BP.NAME BENEFIT_PLAN,
BBR.BENEFIT_RELATION_NAME,
Round(BPER.BNFT_AMT, 2) * 100 COVERAGE_AMOUNT,
TO_CHAR(BPER.ENRT_CVG_STRT_DT, 'YYYYMMDD') ENROLMENTCOVSTARTDATE
TO_CHAR(BPER.ENRT_CVG_THRU_DT, 'YYYYMMDD') ENROLMENTCOVSENDDATE
FROM PER_ALL_PEOPLE_F PAPF,
BEN_PRTT_ENRT_RSLT BPER,
BEN_PL_F BP,
BEN_BENEFIT_RELATIONS_F BBR
WHERE PAPF.PERSON_ID = BPER.PERSON_ID
AND BPER.PL_ID = BP.PL_ID
AND BBR.PERSON_ID = PAPF.PERSON_ID
AND BBR.BENEFIT_RELATION_NAME = 'DFLT'
AND SYSDATE BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BP.EFFECTIVE_START_DATE AND BP.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BBR.EFFECTIVE_START_DATE AND BBR.EFFECTIVE_END_DATE
这会给我这样的输出 -
PErson_number BENEFIT_PLAN COVERAGE_AMOUNT ENROLMENTCOVSTARTDATE ENROLMENTCOVSENDDATE
1010 US Basic PLAN 20000 20200901 20201020
1010 US Basic PLAN 20000 20201021
1011 Us Spouse PLAN 160000 20200901 20201020
1011 Us Spouse PLAN 160000 20201021 47121231
我只想检索每个人的 ENROLMENTCOVSTARTDATE 最大值。预期输出应该是 -
PErson_number BENEFIT_PLAN COVERAGE_AMOUNT ENROLMENTCOVSTARTDATE ENROLMENTCOVSENDDATE
1010 US Basic PLAN 20000 20201021
1011 Us Spouse PLAN 160000 20201021 20201220
如何在主查询中使用 Max?
首先按每个人 ENRT_CVG_STRT_DT 对行进行排名(假设由 PAPF.PERSON_NUMBER 标识)。然后过滤顶部的行。
select person_number,
name,
BENEFIT_PLAN,
BENEFIT_RELATION_NAME,
COVERAGE_AMOUNT,
ENROLMENTCOVSTARTDATE,
ENROLMENTCOVSENDDATE
from (
select
PAPF.PERSON_NUMBER,
BP.NAME BENEFIT_PLAN,
BBR.BENEFIT_RELATION_NAME,
Round(BPER.BNFT_AMT, 2) * 100 COVERAGE_AMOUNT,
TO_CHAR(BPER.ENRT_CVG_STRT_DT, 'YYYYMMDD') ENROLMENTCOVSTARTDATE,
TO_CHAR(BPER.ENRT_CVG_THRU_DT, 'YYYYMMDD') ENROLMENTCOVSENDDATE,
row_number() over (partition by PAPF.PERSON_NUMBER order by BPER.ENRT_CVG_STRT_DT desc) rn
from
FROM PER_ALL_PEOPLE_F PAPF,
BEN_PRTT_ENRT_RSLT BPER,
BEN_PL_F BP,
BEN_BENEFIT_RELATIONS_F BBR
WHERE PAPF.PERSON_ID = BPER.PERSON_ID
AND BPER.PL_ID = BP.PL_ID
AND BBR.PERSON_ID = PAPF.PERSON_ID
AND BBR.BENEFIT_RELATION_NAME = 'DFLT'
AND SYSDATE BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BP.EFFECTIVE_START_DATE AND BP.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BBR.EFFECTIVE_START_DATE AND BBR.EFFECTIVE_END_DATE
)
where rn = 1
如果您想接受并列第一名,那么您可以将 row_number 更改为 rank/dense_rank(如果您在 rn = 1 上过滤则无关紧要)。人们会假设每个人每个 ENROLMENTCOVSTARTDATE 只有一行。
我写了下面的代码-
select
PAPF.PERSON_NUMBER,
BP.NAME BENEFIT_PLAN,
BBR.BENEFIT_RELATION_NAME,
Round(BPER.BNFT_AMT, 2) * 100 COVERAGE_AMOUNT,
TO_CHAR(BPER.ENRT_CVG_STRT_DT, 'YYYYMMDD') ENROLMENTCOVSTARTDATE
TO_CHAR(BPER.ENRT_CVG_THRU_DT, 'YYYYMMDD') ENROLMENTCOVSENDDATE
FROM PER_ALL_PEOPLE_F PAPF,
BEN_PRTT_ENRT_RSLT BPER,
BEN_PL_F BP,
BEN_BENEFIT_RELATIONS_F BBR
WHERE PAPF.PERSON_ID = BPER.PERSON_ID
AND BPER.PL_ID = BP.PL_ID
AND BBR.PERSON_ID = PAPF.PERSON_ID
AND BBR.BENEFIT_RELATION_NAME = 'DFLT'
AND SYSDATE BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BP.EFFECTIVE_START_DATE AND BP.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BBR.EFFECTIVE_START_DATE AND BBR.EFFECTIVE_END_DATE
这会给我这样的输出 -
PErson_number BENEFIT_PLAN COVERAGE_AMOUNT ENROLMENTCOVSTARTDATE ENROLMENTCOVSENDDATE
1010 US Basic PLAN 20000 20200901 20201020
1010 US Basic PLAN 20000 20201021
1011 Us Spouse PLAN 160000 20200901 20201020
1011 Us Spouse PLAN 160000 20201021 47121231
我只想检索每个人的 ENROLMENTCOVSTARTDATE 最大值。预期输出应该是 -
PErson_number BENEFIT_PLAN COVERAGE_AMOUNT ENROLMENTCOVSTARTDATE ENROLMENTCOVSENDDATE
1010 US Basic PLAN 20000 20201021
1011 Us Spouse PLAN 160000 20201021 20201220
如何在主查询中使用 Max?
首先按每个人 ENRT_CVG_STRT_DT 对行进行排名(假设由 PAPF.PERSON_NUMBER 标识)。然后过滤顶部的行。
select person_number,
name,
BENEFIT_PLAN,
BENEFIT_RELATION_NAME,
COVERAGE_AMOUNT,
ENROLMENTCOVSTARTDATE,
ENROLMENTCOVSENDDATE
from (
select
PAPF.PERSON_NUMBER,
BP.NAME BENEFIT_PLAN,
BBR.BENEFIT_RELATION_NAME,
Round(BPER.BNFT_AMT, 2) * 100 COVERAGE_AMOUNT,
TO_CHAR(BPER.ENRT_CVG_STRT_DT, 'YYYYMMDD') ENROLMENTCOVSTARTDATE,
TO_CHAR(BPER.ENRT_CVG_THRU_DT, 'YYYYMMDD') ENROLMENTCOVSENDDATE,
row_number() over (partition by PAPF.PERSON_NUMBER order by BPER.ENRT_CVG_STRT_DT desc) rn
from
FROM PER_ALL_PEOPLE_F PAPF,
BEN_PRTT_ENRT_RSLT BPER,
BEN_PL_F BP,
BEN_BENEFIT_RELATIONS_F BBR
WHERE PAPF.PERSON_ID = BPER.PERSON_ID
AND BPER.PL_ID = BP.PL_ID
AND BBR.PERSON_ID = PAPF.PERSON_ID
AND BBR.BENEFIT_RELATION_NAME = 'DFLT'
AND SYSDATE BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BP.EFFECTIVE_START_DATE AND BP.EFFECTIVE_END_DATE
AND SYSDATE BETWEEN BBR.EFFECTIVE_START_DATE AND BBR.EFFECTIVE_END_DATE
)
where rn = 1
如果您想接受并列第一名,那么您可以将 row_number 更改为 rank/dense_rank(如果您在 rn = 1 上过滤则无关紧要)。人们会假设每个人每个 ENROLMENTCOVSTARTDATE 只有一行。