Oracle:查询如何获得小计和总计一起?

时间:2016-04-20 09:14:20

标签: sql oracle subtotal totals

我使用oracle,我想要查询得到小计和总计一起吗?

示例数据。

SDate      | BankCode | ERCODE | CURRENCY | Rate
21/08/2015 | 12       | AQER   | USD      | 10
21/08/2015 | 12       | AQER   | USD      | 12
21/08/2015 | 12       | BERT   | EUR      | 14
21/08/2015 | 12       | POE    | JPY      | 31
21/08/2015 | 9        | AQER   | USD      | 15
21/08/2015 | 9        | HUIO   | USD      | 08
01/09/2015 | 9        | AQER   | USD      | 45
01/09/2015 | 9        | AQER   | USD      | 11
01/09/2015 | 9        | JIOW   | EUR      | 47

我需要结果。

SDate      | BankCode | ERCODE | CURRENCY | Rate | No.
21/08/2015 | 12       | AQER   | USD      | 10   | 1
21/08/2015 | 12       | AQER   | USD      | 12   | 
Sub Total  |          |        |          | 22   |                
21/08/2015 | 12       | BERT   | EUR      | 14   | 2
21/08/2015 | 12       | POE    | JPY      | 31   | 3
GrandTotal |          |        |          | 67   | 

21/08/2015 | 9        | AQER   | USD      | 15   | 1
21/08/2015 | 9        | HUIO   | USD      | 08   | 2
GrandTotal |          |        |          | 23   | 

01/09/2015 | 9        | AQER   | USD      | 45   | 1
01/09/2015 | 9        | AQER   | USD      | 11   |
Sub Total  |          |        |          | 56   |    
01/09/2015 | 9        | JIOW   | EUR      | 47   | 2
GrandTotal |          |        |          | 103  | 

数据关系: SDATE> BANKCODE>货币> ERCODE

并在新查询后插入No.列。

查询如何一起获得小计和总计?

先谢谢了。 :)

3 个答案:

答案 0 :(得分:2)

这个包含dense_rank()rollupgrouping_id的解决方案可以提供帮助:

with data as (
  select sdate, bankcode, ercode, currency, rate, 
         dense_rank() over (partition by sdate, bankcode order by ercode) rnk
    from t)
select sdate, bankcode, ercode, currency, rate, rnk,
       case when gid = 0 then 'Sub total' when gid = 1 then 'Grand total' end type
  from (
    select sdate, bankcode, ercode, currency, rate, rnk, null gid from data
    union all
    select sdate, bankcode, null, currency, sum(rate), null rnk, 
           grouping_id(sdate, bankcode, currency) rnk
      from data
      group by rollup(sdate, bankcode, currency)
      having (grouping_id(sdate, bankcode, currency) = 0 and currency = 'USD') 
          or grouping_id(sdate, bankcode, currency) = 1 )
    order by sdate, bankcode desc, decode(currency, 'USD', 1, 2), rnk

测试数据和输出:

create table t (SDate date, BankCode number(3), Ercode varchar2(5), 
                Currency varchar2(5), Rate number(6));
insert into t values (date '2015-08-21', 12, 'AQER', 'USD', 10 );
insert into t values (date '2015-08-21', 12, 'AQER', 'USD', 12 );
insert into t values (date '2015-08-21', 12, 'BERT', 'EUR', 14 );
insert into t values (date '2015-08-21', 12, 'POE',  'JPY', 31 );
insert into t values (date '2015-08-21',  9, 'AQER', 'USD', 15 );
insert into t values (date '2015-08-21',  9, 'HUIO', 'USD', 08 );
insert into t values (date '2015-09-01',  9, 'AQER', 'USD', 45 );
insert into t values (date '2015-09-01',  9, 'AQER', 'USD', 11 );
insert into t values (date '2015-09-01',  9, 'JIOW', 'EUR', 47 );

SDATE       BANKCODE ERCODE CURRENCY       RATE        RNK TYPE
----------- -------- ------ -------- ---------- ---------- -----------
2015-08-21        12 AQER   USD              10          1 
2015-08-21        12 AQER   USD              12          1 
2015-08-21        12        USD              22            Sub total
2015-08-21        12 BERT   EUR              14          2 
2015-08-21        12 POE    JPY              31          3 
2015-08-21        12                         67            Grand total
2015-08-21         9 AQER   USD              15          1 
2015-08-21         9 HUIO   USD               8          2 
2015-08-21         9        USD              23            Sub total
2015-08-21         9                         23            Grand total
2015-09-01         9 AQER   USD              45          1 
2015-09-01         9 AQER   USD              11          1 
2015-09-01         9        USD              56            Sub total
2015-09-01         9 JIOW   EUR              47          2 
2015-09-01         9                        103            Grand total

答案 1 :(得分:0)

我的第一个想法是

sum(rate) over (partition by bankcode, currency)

但是有一组EUR / JPY ..你需要更具体地说明你想要完成什么。在我看来,这个小计和总计可以完成,但是在pl / sql f.e中。如果你想获得2行,计算sub,获得接下来的2行,计算grand等等。最重要的是 - 什么是sub sub和grand?

答案 2 :(得分:0)

Rollup必须帮助你

试试这个:

SELECT SDATE, BANKCODE,  CURRENCY, ERCODE,
       SUM(Rate) AS Rate
FROM   your_table
GROUP BY ROLLUP (SDATE, BANKCODE,  CURRENCY, ERCODE)
ORDER BY SDATE, BANKCODE,  CURRENCY, ERCODE;