简体   繁体   English

SQL Oracle /聚合查询

[英]SQL Oracle/Aggregation query

I'm trying to run a query on Oracle. 我正在尝试在Oracle上运行查询。 I've a table of settled payments for accounts, I have a query which pulls through the last three settled amounts, plus any amount which was written off, for any account I need this info for. 我有一个帐户的结算付款表,我有一个查询,它查询我需要此信息的任何帐户的最后三个结算金额以及已注销的任何金额。

However, some of the accounts are weekly based, and for these I would like to aggregate their weekly settlements into their monthly groups. 但是,有些帐户是每周结算的,对于这些帐户,我想将其每周结算汇总到其每月组中。 Here is the code I have so far: 这是我到目前为止的代码:

SELECT   *
FROM     (
    SELECT *
    FROM   (
            SELECT   gwod.account_id,
                     gwod.charge_period_start,
                     SUM(gwod.total_due_on_charge) total_due_on_charge,
                     SUM(gwod.amount_written_off) amount_written_off,
                     DENSE_RANK() over (PARTITION BY gwod.account_id 
                                        ORDER BY charge_period_start DESC) rownumber
            FROM     report.accounts_write_off gwod
            WHERE    account_id IN (‘account_number’)
            GROUP BY gwod.account_id,
                     gwod.charge_period_start
            HAVING   SUM (gwod.total_due_on_charge) <> 0) t1
    WHERE  t1.rownumber <=3) 
    PIVOT (MAX(charge_period_start) charge_period, 
           MAX(total_due_on_charge) total_due_on_charge, 
           MAX(amount_written_off) amount_written_off 
           FOR rownumber IN (1,2,3))
ORDER BY account_id.* 

This works perfectly but for the weekly based accounts, so rather than pulling through the last three weekly amounts which were settled, ie 25-09-17, 18-09-17, 11-09-2017, I'd like to pull through the aggregated payments for September, August, and July. 这非常有效,但是对于基于每周的帐户,因此我不想跳过过去的三个每周已结算的金额,即25-09-17、18-09-17、11-09-2017, 9月,8月和7月的总付款额。

I hope all this makes sense. 我希望所有这些都是有道理的。

Simply change your aggregation from current unit level (ie, weekly) to month level with EXTRACT(month ...) in inner query's SELECT and GROUP BY as well as PARTITION and PIVOT clauses: 只需在内部查询的SELECTGROUP BY以及PARTITIONPIVOT子句中使用EXTRACT(month ...)将汇总从当前单位级别(即每周)更改为月份级别:

SELECT   *
FROM     (
    SELECT *
    FROM   (
            SELECT   gwod.account_id,
                     EXTRACT(month FROM gwod.charge_period_start) charge_period_month,
                     SUM(gwod.total_due_on_charge) total_due_on_charge,
                     SUM(gwod.amount_written_off) amount_written_off,
                     DENSE_RANK() over (PARTITION BY gwod.account_id 
                                        ORDER BY EXTRACT(month FROM gwod.charge_period_start) DESC) rownumber
            FROM     report.accounts_write_off gwod
            WHERE    account_id IN ('account_number')
            GROUP BY gwod.account_id,
                     EXTRACT(month FROM gwod.charge_period_start)
            HAVING   SUM (gwod.total_due_on_charge) <> 0) t1
    WHERE  t1.rownumber <=3) 
    PIVOT (MAX(charge_period_month) charge_period, 
           MAX(total_due_on_charge) total_due_on_charge, 
           MAX(amount_written_off) amount_written_off 
           FOR rownumber IN (1,2,3))
ORDER BY account_id.* 

DEMO (with random data) : 演示 (带有随机数据)

http://rextester.com/UJK84858 http://rextester.com/UJK84858

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM