简体   繁体   English

Oracle 11g中的自联接表

[英]Self Join Table in Oracle 11g

Suppose I have a table resulted from a query like this: 假设我有一个像这样的查询产生的表:

+-------+-----+--------------------+-----+--------------------+
|CODE   |CURR |FRONT-END CHARGE    |CCY  |BACK-END CHARGE     |
+-------+-----+--------------------+-----+--------------------+
|002    |AUD  |5.25                |PHP  | 3.75               |
|002    |AUD  |1.75                |USD  | 1.25               |
|002    |BGN  |  14                |PHP  | 8.75               |
|002    |BGN  |   6                |USD  | 3.75               |
|002    |BND  | 9.5                |PHP  |  8.5               |
|002    |BND  |4.25                |USD  |12.75               |
|002    |CAD  |12.5                |USD  | 6.75               |
|002    |INR  |  35                |PHP  |22.75               |
|002    |INR  |  25                |USD  |16.25               |
|002    |YEN  |55.5                |PHP  |16.55               |
|002    |YEN  |77.5                |USD  | 39.2               |
+-------+-----+--------------------+-----+--------------------+

But I want to have a result like this: 但我希望得到这样的结果:

+-------+-----+--------------------+-----+--------------------+
|CODE   |CURR |FRONT-END CHARGE    |CCY  |BACK-END CHARGE     |
+-------+-----+--------------------+-----+--------------------+
|002    |AUD  |7                   |PHP  | 3.75               |
|002    |     |                    |USD  | 1.25               |
|002    |BGN  |20                  |PHP  | 8.75               |
|002    |     |                    |USD  | 3.75               |
|002    |BND  |13.75               |PHP  |  8.5               |
|002    |     |                    |USD  |12.75               |
|002    |CAD  |12.5                |USD  | 6.75               |
|002    |INR  |60                  |PHP  |22.75               |
|002    |     |                    |USD  |16.25               |
|002    |YEN  |133                 |PHP  |16.55               |
|002    |     |                    |USD  | 39.2               |
+-------+-----+--------------------+-----+--------------------+

Note that the Front-end charge is the sum of every charges on each of the currency. 请注意,前端费用是每种货币的每笔费用的总和。

I tried using coalesce but it returns the same table when selecting. 我尝试使用coalesce但它在选择时返回相同的表。 I also tried self join but the sum on each of the charges becomes different. 我也尝试过自我加入,但每项收费的总和都不同了。 This is in oracle 11g 这是在oracle 11g

This type of transformation should usually be done in the presentation layer -- because the results are not really a SQL table: rows are missing column values. 这种类型的转换通常应该在表示层中完成 - 因为结果实际上不是SQL表:行缺少列值。

But, you can do this using window functions: 但是,您可以使用窗口函数执行此操作:

select code,
       (case when row_number() over (partition by code, curr order by front_end_charge desc) = 1
             then code
        end) as code,
       (case when row_number() over (partition by code, curr order by front_end_charge desc) = 1
             then sum(front_end_charge) over (partition by code, curr)
        end) as front_end_charge,
       ccy, back_end_charge
from t
order by t.code, t.curr, t.front_end_charge desc;

It is important that the outer order by match the order by in the row_number() expressions. 重要的是外部order by匹配row_number()表达式中的order by SQL queries only return results in a determined order when order by is present. order by存在时,SQL查询仅以确定的顺序返回结果。

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

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