如何知道oracle中事务的统计日期和结束日期

时间:2017-09-29 15:51:24

标签: oracle

我有一张桌子:

Ename Deptno JoinDate
RAKESH 10     01-OCT-90
RAKESH 20     01-DEC-91
RAKESH 30     01-MAR-92

在此查询中一个特定的员工已加入加入不同的Deptno组织我需要知道他的终止日期,其中的日期介于10到30之间的加入日期

O / P:

Ename     Deptno    StartDate     Enddate        Working Days
RAKESH   10      01-OCT-90     30-NOV-91        425
RAKESH   20      01-DEC-91     29-Feb-92        90
RAKESH   30      01-MAR-92     Still Working    9317

2 个答案:

答案 0 :(得分:2)

你去了

Sub Extract_Bank_Amount2()

    Dim cell As Range
    With Worksheets("Bank Statement")

        For Each cell In .Range("B" & .Rows.Count).End(xlUp)

            If cell.Offset(0, -1).Value = Range("PayrollB1").Value Then
                If cell.Offset(0, 1).Value = Range("PayrollB3").Value Then

                    With wb.Sheets("Proof")
                        .Range("C" & .Rows.Count).End(xlUp).Offset(1).Value = cell.Value
                    End With

                End If
            End If
        Next
    End With

End Sub

结果:

-- just sample of data from your question
with t1(Ename, Deptno, JoinDate) as(
  select 'RAKESH', 10, to_date('01-OCT-90', 'dd-mon-rr') from dual union all
  select 'RAKESH', 20, to_date('01-DEC-91', 'dd-mon-rr') from dual union all
  select 'RAKESH', 30, to_date('01-MAR-92', 'dd-mon-rr') from dual
)
-- actual query
select ename
     , deptno
     , to_char(joindate, 'dd-mon-rr') as startdate
     , nvl(to_char(jdate - 1, 'dd-mon-rr'), 'Still Working...') as enddate
     , (nvl(jdate - 1, sysdate) - joindate) as working_days
  from (
        select ename
             , deptno
             , joindate
             , lead(joindate, 1) over(partition by ename 
                                      order by joindate) as jdate
         from t1 q )

答案 1 :(得分:0)

实际上你不需要子查询。 2个单独的Lead函数将为您提供输出。假设您的日期列实际上是date数据类型,而不是字符串。否则,您必须使用to_date将其转换为date

此外,显示“仍在工作”'在日期列中不可建议。如果是出于报告目的,则将EndDate显示为NULL将解决您的目的。但无论如何,如果你想这样做,那么使用to_char函数将EndDate转换为char,然后使用NVL,如Nicholas所示。

select ename,deptno,JoinDate,
    lead(JoinDate,1) over(partition by ename order by joindate) - 1
    as enddate
    ,FLOOR(
            nvl(lead(JoinDate,1) over(partition by ename order by joindate) 
            ,sysdate
                ) 
            - joindate 
          )
    as WorkingDays
from tbl;

Fiddle Demo

输出

ENAME   DEPTNO  JOINDATE    ENDDATE     WORKINGDAYS
RAKESH  10      01-OCT-90   30-NOV-91   426
RAKESH  20      01-DEC-91   29-FEB-92   91
RAKESH  30      01-MAR-92   null        9343

说明:LEAD将为您提供下一行结果(由分区依据和按顺序定义)。您可以使用它来使用end_date的当前行中的下一个日期。您可以减去EndDateJoinDate以获得输出。