在ORACLE

时间:2017-08-16 06:31:46

标签: java oracle

我有一个像这样的表(交易)

ID   | Name  |   Unit  |Quantity_in|Quantity_out|transaction_date|
-----|-------| --------|-----------|------------|----------------|
100  |  aaaa |  packet |  100      |    0       |     02/07/2017 |
99   |  cccc |  packet |  70       |    0       |     20/06/2017 |
99   |  cccc |  packet |  0        |    20      |     22/07/2017 |
100  |  aaaa |  strip  |   0       |    30      |     05/07/2017 |
102  |  bbbb |  packet |  50       |    0       |     29/06/2017 |

我想进行查询以获取两天之间的交易和当前余额,同时在WHERE条件中的日期之前获得先前的余额,因此我进行以下查询

从事务tr中选择d.ID,d.Name,d。单位,(选择sum(tr.Quantity_in) - sum(tr.Quantity_out),其中tr.transaction_date< 01/07 / 2017年和d.ID = tr.ID)作为Prev_bal,sum(d.Quantity_in),sum(d.Quantity_out),Prev_bal +(sum(d.Quantity_in) - sum(d.Quantity_out))as current_balance来自交易d 在2017年7月1日至2017年7月30日期间d.transaction_date 按d.ID分组,d.Name,d。单位

但查询下面的结果不包含ID 102,因为Where条件中的日期之间没有交易

ID  | Name | Unit   | Prev_bal | Quantity_in |  Quantity_out |Current_balance|
----|------| ------ |----------|-------------|---------------|---------------|    
100 |aaaa  |packet  |   0      |  100        |    30         |     70
----|----- |--------|----------|-------------|---------------|---------------|
99  |cccc  |packet  |   70     |  0          |    20         |     50        |

任何人都可以帮助我进行查询,即使WHERE条件中的日期之间没有事务处理(如表中的ID 102),也会获得所有具有余额的ID的结果。      我需要获得当前余额(Current_balance),其中2个日期之间的transaction_date和之前的余额(Prev_bal)用于具有balance_date<的余额的ID。在哪里条件的日期 在此先感谢您的帮助。 结果应该像下面那样

ID |名称|单位| Prev_bal | Quantity_in | Quantity_out | Current_balance |

100 | aaaa |包| 0 | 100 | 30 | 70

99 | cccc |包| 70 | 0 | 20 | 50 |

102 | bbbb |包| 50 | 0 | 0 | 50 |

1 个答案:

答案 0 :(得分:0)

所以,这就是我的想法。

无论您提供的日期范围如何,您仍希望每个ID都包含在结果集中。因此无需过滤掉行。所以问题是现在只是在一天结束时找出剩余的余额。因此,对于日期范围内的项目,余额是当前余额,对于日期范围之前的项目,它是之前的余额(基本上是最后一个当前余额)。 以下是使用Oracle中的分析函数的查询。

WITH dat
     AS (SELECT 100 AS id,
                'aaaa' AS name,
                'packet' AS unit,
                100 AS quantity_in,
                0 AS quantity_out,
                TO_DATE ('02/07/2017', 'DD/MM/YYYY') AS transaction_date
         FROM   DUAL
         UNION
         SELECT 99 AS id,
                'cccc' AS name,
                'packet' AS unit,
                70 AS quantity_in,
                0 AS quantity_out,
                TO_DATE ('20/06/2017', 'DD/MM/YYYY') AS transaction_date
         FROM   DUAL
         UNION
         SELECT 99 AS id,
                'cccc' AS name,
                'packet' AS unit,
                0 AS quantity_in,
                20 AS quantity_out,
                TO_DATE ('22/07/2017', 'DD/MM/YYYY') AS transaction_date
         FROM   DUAL
         UNION
         SELECT 100 AS id,
                'aaaa' AS name,
                'strip' AS unit,
                0 AS quantity_in,
                30 AS quantity_out,
                TO_DATE ('05/07/2017', 'DD/MM/YYYY') AS transaction_date
         FROM   DUAL
         UNION
         SELECT 102 AS id,
                'bbbb' AS name,
                'packet' AS unit,
                50 AS quantity_in,
                0 AS quantity_out,
                TO_DATE ('29/06/2017', 'DD/MM/YYYY') AS transaction_date
         FROM   DUAL)
SELECT id,
       name,
       unit,
       quantity_in,
       quantity_out,
       transaction_date,
       remaining_balance
FROM   (SELECT id,
               name,
               unit,
               quantity_in,
               quantity_out,
               balance,
               transaction_date,
               SUM (balance)
               OVER (PARTITION BY id ORDER BY transaction_date ROWS UNBOUNDED PRECEDING)
                  AS remaining_balance,
               RANK () OVER (PARTITION BY id ORDER BY transaction_date DESC) AS rn
        FROM   (SELECT id,
                       name,
                       unit,
                       quantity_in,
                       quantity_out,
                       quantity_in
                       - quantity_out
                          AS balance,
                       transaction_date
                FROM   dat))
WHERE  rn = 1;

它产生了这个结果。

ID |NAME|UNIT   |  QUANTITY_IN|QUANTITY_OUT|TRANSACTION_DATE|REMAINING_BALANCE
99 |cccc|packet |            0|          20|       7/22/2017|               50
100|aaaa|strip  |            0|          30|        7/5/2017|               70
102|bbbb|packet |           50|           0|       6/29/2017|               50