您好我需要获得上一季度交易的交易数量和总金额。 我使用以下来获取四分之一(过去90天)的数据
WITH date_range as
(SELECT TRUNC(sysdate) - 90 + level AS week_day
FROM dual
CONNECT BY ROWNUM <= 90),
the_data
AS (SELECT TRUNC(systemdate) AS log_date, count(*) AS num_obj,status AS log_status, nvl(sum(
CASE
WHEN VERSION = '1.1'
THEN nvl(amount/100,'0.0')
ELSE nvl(amount,'0.0')
END), 0) AS totalamount
from transactionlog where ((merchantcode in (
SELECT regexp_substr('MERC0003','[^,]+', 1, LEVEL) FROM dual
connect by regexp_substr('MERC0003', '[^,]+', 1, level) is not null ) OR 'MERC0003' IS NULL) AND status = 'xxxx')
GROUP BY TRUNC(systemdate),status)
SELECT TO_CHAR(dr.week_day,'DD/MM/YYYY HH:MI AM') AS TXNDATE, NVL(trans_log.num_obj,0) as TXNCOUNT,trans_log.log_status,trans_log.totalamount
FROM date_range dr LEFT OUTER JOIN the_data trans_log
on trans_log.log_date = dr.week_day
ORDER BY dr.week_day DESC ;
从上面,我获得了90天的记录,如90行包含交易计数和金额90天, 我需要按周计算数据。也就是说,包含每周数据的12行,其中包含过去12周内的交易计数和金额。
答案 0 :(得分:0)
您可以使用TRUNC( date_value ,'IW' )
截断ISO周开始日期(星期一00:00)并在日期范围内添加一周的倍数,并在查询中执行相同的截断:
WITH date_range( week_day ) as (
SELECT TRUNC( sysdate - 90, 'IW' ) + ( level - 1 ) * INTERVAL '7' DAY
FROM dual
CONNECT BY TRUNC( sysdate - 90, 'IW' ) + ( level - 1 ) * INTERVAL '7' DAY <= SYSDATE
),
the_data ( log_date, num_obj, log_status, totalamount ) AS (
SELECT TRUNC(systemdate, 'IW'),
-- rest of your query
GROUP BY TRUNC(systemdate, 'IW'),
status
)
SELECT *
FROM date_range dr
LEFT OUTER JOIN the_data trans_log
on trans_log.log_date = dr.week_day
ORDER BY dr.week_day DESC;