将特定日期的某些行与数据分组

时间:2018-09-04 20:16:13

标签: sql oracle group-by grouping window-functions

有这样一个操作表:

+----+------------+-----------+--------+---------------------------------+---------------------------------+---------------------------------+
| id | oper_date  | parent_id | amount |               p1                |               p2                |               p3                |
+----+------------+-----------+--------+---------------------------------+---------------------------------+---------------------------------+
|  1 | 01.09.2018 |         1 |      5 | p1 value at the date 01.09.2018 | p2 value at the date 01.09.2018 | p3 value at the date 01.09.2018 |
|  2 | 01.09.2018 |           |      2 | p1 value at the date 01.09.2018 | p2 value at the date 01.09.2018 | p3 value at the date 01.09.2018 |
|  3 | 02.09.2018 |         1 |      7 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  4 | 02.09.2018 |           |      4 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  5 | 02.09.2018 |           |      6 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  6 | 02.09.2018 |           |     10 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  7 | 03.09.2018 |           |      4 | p1 value at the date 03.09.2018 | p2 value at the date 03.09.2018 | p3 value at the date 01.09.2018 |
|  8 | 03.09.2018 |         1 |      3 | p1 value at the date 03.09.2018 | p2 value at the date 03.09.2018 | p3 value at the date 01.09.2018 |
|  9 | 03.09.2018 |           |      2 | p1 value at the date 03.09.2018 | p2 value at the date 03.09.2018 | p3 value at the date 01.09.2018 |
| 10 | 04.09.2018 |           |      6 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 11 | 04.09.2018 |         1 |      7 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 12 | 04.09.2018 |           |     11 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 13 | 04.09.2018 |           |      4 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 14 | 05.09.2018 |           |      8 | p1 value at the date 05.09.2018 | p2 value at the date 05.09.2018 | p3 value at the date 05.09.2018 |
| 15 | 05.09.2018 |         1 |      2 | p1 value at the date 05.09.2018 | p2 value at the date 05.09.2018 | p3 value at the date 05.09.2018 |
| 16 | 05.09.2018 |           |      6 | p1 value at the date 05.09.2018 | p2 value at the date 05.09.2018 | p3 value at the date 05.09.2018 |
+----+------------+-----------+--------+---------------------------------+---------------------------------+---------------------------------+

获取oper_date为<=特定日期的行时, parent_id字段具有相同非空值的几行 以便在amount字段中显示该日期的所有操作的总和。

在字段p1/p2/p3/id/oper_date中,我需要显示此日期的这些字段的值。

例如,如果我选择oper_date <= 04.09.2018的行,则需要显示以下结果:

+----+------------+-----------+--------+---------------------------------+---------------------------------+---------------------------------+
| id | oper_date  | parent_id | amount |               p1                |               p2                |               p3                |
+----+------------+-----------+--------+---------------------------------+---------------------------------+---------------------------------+
|  2 | 01.09.2018 |           |      2 | p1 value at the date 01.09.2018 | p2 value at the date 01.09.2018 | p3 value at the date 01.09.2018 |
|  4 | 02.09.2018 |           |      4 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  5 | 02.09.2018 |           |      6 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  6 | 02.09.2018 |           |     10 | p1 value at the date 02.09.2018 | p2 value at the date 02.09.2018 | p3 value at the date 01.09.2018 |
|  7 | 03.09.2018 |           |      4 | p1 value at the date 03.09.2018 | p2 value at the date 03.09.2018 | p3 value at the date 01.09.2018 |
|  9 | 03.09.2018 |           |      2 | p1 value at the date 03.09.2018 | p2 value at the date 03.09.2018 | p3 value at the date 01.09.2018 |
| 10 | 04.09.2018 |           |      6 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 11 | 04.09.2018 |         1 |     22 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 12 | 04.09.2018 |           |     11 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
| 13 | 04.09.2018 |           |      4 | p1 value at the date 04.09.2018 | p2 value at the date 04.09.2018 | p3 value at the date 04.09.2018 |
+----+------------+-----------+--------+---------------------------------+---------------------------------+---------------------------------+

对于id = 11的行,字段AMOUNT显示值的总和(22 = 5 + 7 + 3 + 7) parent_id = 1并且oper_date为<= 04/09/2018的所有行中的所有行。

如何使用sql-query执行此操作?

P.S。设计表时,对于parent_id字段中的父行,我可以保留null或其id。

2 个答案:

答案 0 :(得分:0)

假设您已将感兴趣的日期(在本例中为04/09/2018)另存为@SetDate。根据您的预期输出,我想您希望所有在parent_id或之前带有NULL @SetDate的记录,尤其是要返回@SetDate的记录,而您想要@SetDate上和之前的所有金额之和,其中parent_id作为单独记录不为NULL。

SELECT id, oper_date, parent_id, amount, p1, p2, p3
FROM your_table 
WHERE parent_id IS NULL AND oper_date <= @SetDate

UNION

SELECT 
(SELECT id FROM your_table WHERE oper_date = @SetDate AND parent_id = 1 LIMIT 1) AS id, 
@SetDate AS oper_date, 
1 AS parent_id, 
(SELECT SUM(Amount) FROM your_table WHERE oper_date <= @SetDate AND parent_id = 1) AS amount,
(SELECT p1 FROM your_table WHERE oper_date = @SetDate AND parent_id = 1 LIMIT 1) AS p1,
(SELECT p2 FROM your_table WHERE oper_date = @SetDate AND parent_id = 1 LIMIT 1) AS p2,
(SELECT p3 FROM your_table WHERE oper_date = @SetDate AND parent_id = 1 LIMIT 1) AS p3
FROM your_table

答案 1 :(得分:0)

在此,“解析函数划分依据”将很有帮助。使用按ID排序将使总和递增,最大ID将具有总和。按照您的解释

使用MY_VIEW AS   (SELECT id,oper_date,parent_id,SUM(amount)超过(partition by parent_id order by id)AS数量,p1,p2,p3
  来自my_table   如果parent_id不为NULL并且oper_date <=   ) SELECT * FROM MY_VIEW WHERE id =   (从MY_VIEW中选择MAX(id)个   )   联合

SELECT id,oper_date,parent_id,amount,p1,p2,p3
  来自my_table   其中parent_id为NULL并且oper_date <=   按ID排序