通过比较列与前一列来选择记录

时间:2014-02-11 20:32:20

标签: sql oracle

我需要找到分配给99001的个人资料的人,但前提是他们之前被分配到以0结尾的个人资料(但不是全部为零)。当前配置文件的有效日期与之前的到期日相匹配。

我的代码没有返回任何记录:

    SELECT p.personnum, 
    ap.name, 
    aa.effectivedtm,
    aa.expirationdtm
    FROM vp_employeev42 p,
    ASSIGNACCRUAL aa,  
    ACCRUALPROFILE ap
    WHERE p.personid=aa.employeeid
    and aa.ACCRUALPROFILEID= ap.ACCRUALPROFILEID
    and p.employmentstatus = 'Active'
    and p.ACCRUALPRFLNAME = '99001'
    and ap.name = '99001' 
    and aa.EFFECTIVEDTM = (
    select t2.expirationdtm from assignaccrual t2 
   where t2.employeeid = p.personid 
   and t2.accrualprofileid = ap.ACCRUALPROFILEID 
   and ap.NAME like '%0' 
   and ap.name != '00000')
   order by p.PERSONNUM, aa.EFFECTIVEDTM

任何帮助都会很棒。

ASSIGNACCRUAL-Sample

EMPLOYEEID  ACCRUALPROFILEID    EFFECTIVEDTM    EXPIRATIONDTM   UPDATEDBYUSRACCTID  UPDATEDTM   VERSIONCNT
3778    33482   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3779    33483   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
403019  34285   1   1-Jan-53    1-Jan-14    -10 29-Dec-13   1
3781    33485   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3782    33486   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3783    33487   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3784    33488   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3785    33489   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3788    33492   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
403021  65065   1   1-Jan-53    1-Jan-14    -10 29-Dec-13   1
3790    33494   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
3792    33496   1   1-Jan-53    1-Jan-00    -1  26-Mar-11   1
403023  39566   1   1-Jan-53    1-Jan-14    -10 29-Dec-13   1

ACCRUALPROFILE-样品

ACCRUALCODEID   ADVANCEDSW  NAME    VERSION
    1       0   Default 9
    202     0   4040    1
    203     0   4042    3
    2       0   0   3
    3       0   1010    10
    4       0   1011    5
    5       0   1012    3
    6       0   2000    4
    7       0   2001    3
    8       0   2010    4

如果我删除最后2个限定符,这就是我所看到的。

1004719 Default 01-JAN-53   01-JAN-14
1004719 01010   01-JAN-14   28-JAN-14
1004719 99001   28-JAN-14   01-JAN-00
1005256 Default 01-JAN-53   01-JAN-14
1005256 02010   01-JAN-14   01-FEB-14
1005256 99001   01-FEB-14   01-JAN-00
1008323 Default 01-JAN-53   01-JAN-14
1008323 01010   01-JAN-14   07-FEB-14
1008323 99001   07-FEB-14   01-JAN-00
1014598 Default 01-JAN-53   01-JAN-14
1014598 02010   01-JAN-14   28-JAN-14
1014598 99001   28-JAN-14   01-JAN-00
1024852 Default 01-JAN-53   01-JAN-14
1024852 01010   01-JAN-14   28-JAN-14

1 个答案:

答案 0 :(得分:0)

您可以使用lag()功能获取以前的名称。您需要在子查询中执行此操作,因此where过滤条件不会影响上一条记录的定义。

如果我理解你的查询,那么这应该是你需要的东西(或接近它的东西):

select personnum, name, effectivedtm, expirationdtm
from (SELECT p.personnum, ap.name, aa.effectivedtm, aa.expirationdtm,
             p.employementstatus, p.ACCRUALPRFLNAME, ap.name,
             lag(ap.name) over (partition by p.personnum order by aa.effectivedtm) as prevname
      FROM ASSIGNACCRUAL aa join 
           ACCRUALPROFILE ap
           on aa.ACCRUALPROFILEID = ap.ACCRUALPROFILEID join
           vp_employeev42 p
           on p.personid = aa.employeeid
     ) t
where employmentstatus = 'Active' and
      ACCRUALPRFLNAME = '99001' and
      name = '99001' and
      prevname like '%0' and
      prevname <> '00000'
order by PERSONNUM, EFFECTIVEDTM;