Sql 选择“查询付款计划”

Sql 选择“查询付款计划”,sql,oracle,Sql,Oracle,付款时间表上有一个表格: | PaySum | PlanDate | +----------+------------+ | 23928.38 | 14.10.2019 | | 24347.13 | 12.11.2019 | | 24773.20 | 12.12.2020 | | 25206.73 | 13.01.2020 | 需要在3个月内提取未来金额 例如,我的请求: select sum(s.PaySum) from L_DEA s where s.PlanDate

付款时间表上有一个表格:

| PaySum   | PlanDate   |
+----------+------------+ 
| 23928.38 | 14.10.2019 |
| 24347.13 | 12.11.2019 |
| 24773.20 | 12.12.2020 |
| 25206.73 | 13.01.2020 |   
需要在3个月内提取未来金额

例如,我的请求:

select sum(s.PaySum)
  from L_DEA s
 where s.PlanDate between trunc(sysdate + 1) and
       ADD_months(trunc(sysdate + 1), 3)
   and ID = :iId;    
如果run sysdate=12.10.19或13.10.19,此查询将返回4个月 在其他情况下,3个月内显示正确


如何正确选择表单

也许可以尝试更改不等式:

SELECT SUM(s.PaySum)
FROM L_DEA s
WHERE
    s.PlanDate >= TRUNC(SYSDATE) AND
    s.PlanDate < ADD_MONTHS(TRUNC(SYSDATE + 1), 3) AND
    ID = :iId;

这将包括今天午夜或之后,但三个月后午夜之前的所有计划日期。

我不同意你的看法。至少在给定的样本数据中,它工作正常

2019年10月12日

SQL> with L_DEA (PaySum, PlanDate) as
  2  (select 23928.38 , to_date('14.10.2019','dd.mm.yyyy') from dual union all
  3  select 24347.13 , to_date('12.11.2019','dd.mm.yyyy') from dual union all
  4  select 24773.20 , to_date('12.12.2020','dd.mm.yyyy') from dual union all
  5  select 24773.20 , to_date('10.02.2020','dd.mm.yyyy') from dual union all -- added this
  6  select 25206.73 , to_date('13.01.2020','dd.mm.yyyy') from dual )
  7  select * --sum(s.PaySum)
  8    from L_DEA s
  9   where s.PlanDate between trunc(date '2019-10-12' + 1) and
 10         ADD_months(trunc(date '2019-10-12' + 1), 3)
 11     --and ID = :iId;

    PAYSUM PLANDATE
---------- ---------
  23928.38 14-OCT-19
  24347.13 12-NOV-19
  25206.73 13-JAN-20

SQL>
另一日期,即2019年10月13日

SQL> with L_DEA (PaySum, PlanDate) as
  2  (select 23928.38 , to_date('14.10.2019','dd.mm.yyyy') from dual union all
  3  select 24347.13 , to_date('12.11.2019','dd.mm.yyyy') from dual union all
  4  select 24773.20 , to_date('12.12.2020','dd.mm.yyyy') from dual union all
  5  select 24773.20 , to_date('10.02.2020','dd.mm.yyyy') from dual union all -- added this
  6  select 25206.73 , to_date('13.01.2020','dd.mm.yyyy') from dual )
  7  select * --sum(s.PaySum)
  8    from L_DEA s
  9   where s.PlanDate between trunc(date '2019-10-13' + 1) and
 10         ADD_months(trunc(date '2019-10-13' + 1), 3)
 11     --and ID = :iId;

    PAYSUM PLANDATE
---------- ---------
  23928.38 14-OCT-19
  24347.13 12-NOV-19
  25206.73 13-JAN-20

SQL>

干杯

从今天开始到三个月后的一个简短选项是:

select sum(s.PaySum)
  from l_dea s
 where s.PlanDate between trunc(systimestamp) and trunc(systimestamp) + interval '3' month
   and ID = :iId
每当您需要生成的序列列时,更喜欢使用row_number而不是rownum。如果在同一查询中ORDERBY子句后跟ROWNUM,则ORDERBY子句将对行重新排序。结果可能因访问行的方式而异。
select s.PlanDate, s.PaySum
  from L_DEA s
 inner join (select PlanDate
               from (select distinct s.PlanDate
                       from L_DEA s
                      where  ID = :iId
                        and s.PlanDate >= (to_date('10.10.2019'))
                      order by s.PlanDate) tt
              where rownum <= 3) A2
    on s.PlanDate = A2.PlanDate
 where ID = :iId);