通过在oracle

时间:2017-08-09 11:23:22

标签: sql oracle

我希望在一天内检索现金存款超过4总计1000000的记录,并持续超过5天。

我提出了以下问题。

    SELECT COUNT(a.txamt) AS "txcount"
           , SUM(a.txamt) AS "txsum"
           , b.custcd
           , a.txdate
      FROM tb_transactions a 
INNER JOIN tb_accounts b 
        ON a.acctno = b.acctno
     WHERE a.cashflowtype = 'CR'
     GROUP BY b.custcd, a.txdate 
    HAVING COUNT(a.txamt)>4 and SUM(a.txamt)>='1000000'
     ORDER BY a.txdate;

但是,如果模式持续5天,我就会坚持如何获取记录。

如何达到预期效果?

1 个答案:

答案 0 :(得分:1)

类似的东西:

SELECT *
FROM   (
  SELECT t.*,
         COUNT( txdate ) OVER ( PARTITION BY custcd
                                ORDER BY txdate
                                RANGE BETWEEN INTERVAL '0' DAY PRECEDING
                                          AND INTERVAL '4' DAY FOLLOWING ) AS 
num_days
  FROM   (
    select count(a.txamt) as "txcount",
           sum(a.txamt) as "txsum",
           b.custcd,
           a.txdate
    from   tb_transactions a inner join tb_accounts b on a.acctno=b.acctno
    where  a.cashflowtype='CR'
    group by b.custcd, a.txdate
    having count(a.txamt)>4 and sum(a.txamt)>=1000000
  ) t
)
WHERE num_days = 5
order by a.txdate;