我有以下选择查询,它返回以下数据:
查询:
SELECT DISTINCT ALLVW.GP_PAYGROUP,
ALLVW.PRD_END_DT,
TMP.PRD_END_DT
FROM PS_AZ_DFN_ALPRD_VW ALLVW,
PS_AZ_DFMPRP_TMP4 TMP
WHERE ALLVW.GP_PAYGROUP = TMP.GP_PAYGROUP
AND ALLVW.PRD_END_DT < TMP.PRD_END_DT
结果:
PAYGROUP PRD_END_DT PRD_END_DT_1
AMZ_PG_T1 31-JAN-12 31-DEC-12
AMZ_PG_T1 29-FEB-12 31-DEC-12
AMZ_PG_T1 31-MAR-12 31-DEC-12
AMZ_PG_T1 30-NOV-12 31-DEC-12
AMZ_PG_T2 31-JAN-12 31-OCT-12
AMZ_PG_T2 30-SEP-12 31-OCT-12
AMZ_PG_T2 31-MAR-12 31-OCT-12
AMZ_PG_T2 30-APR-12 31-OCT-12
现在,我希望每个PRD_END_DT
的{{1}}是PAYGROUP
之前的'x'个句点,例如x = 1,我的查询应该返回:< / p>
PRD_END_DT_1
我厌倦了以下但没有全部帮助:
PAYGROUP PRD_END_DT PRD_END_DT_1
AMZ_PG_T1 30-NOV-12 31-DEC-12
AMZ_PG_T2 30-SEP-12 31-OCT-12
请你建议可行的方法。感谢您的意见。
答案 0 :(得分:0)
SELECT * FROM (
your original SELECT DISTINCT... query
) t where datediff(month, PRD_END_DT, PRD_END_DT_1) = x
答案 1 :(得分:0)
SELECT DISTINCT ALLVW.GP_PAYGROUP, ALLVW.PRD_END_DT, TMP.PRD_END_DT
FROM PS_AZ_DFN_ALPRD_VW ALLVW
JOIN PS_AZ_DFMPRP_TMP4 TMP ON ALLVW.GP_PAYGROUP = TMP.GP_PAYGROUP
WHERE ALLVW.PRD_END_DT < TMP.PRD_END_DT
AND add_months(trunc(ALLVW.PRD_END_DT, 'month'), 1) >= trunc(TMP.PRD_END_DT, 'month')
答案 2 :(得分:0)
尝试并使用dense_rank
功能:
E.g。
Select a, b, c
From ( select a, b, c, dense_rank() over (partition by a order by b desc) r
from table)
Where r < :x