对于整个审核跟踪,我试图选择在每周日期序列中每个日期(或之前)最近的记录。我需要在两个表上执行此操作,然后将序列中每个日期的结果合并在一起
我能够在python中做到这一点,但是它是如此之慢,所以我试图直接在postgres中做到这一点,但是我对循环/函数等非常陌生。我知道我会生成一个日期序列执行以下操作:
SELECT d::date
FROM generate_series(TIMESTAMP WITHOUT TIME ZONE '2019-06-03' -- START DATE
,TIMESTAMP WITHOUT TIME ZONE '2019-10-17' -- END DATE
,'7 day') AS gs(d);
这是开始数据的样子
表1:
+--------+--------------+--------+-----+
| idA | history_date | status | idB |
+--------+--------------+--------+-----+
| 123456 | 2019-09-15 | 1 | 1 |
+--------+--------------+--------+-----+
| 123456 | 2019-09-17 | 2 | 1 |
+--------+--------------+--------+-----+
| 654321 | 2019-09-01 | 3 | 5 |
+--------+--------------+--------+-----+
| 654321 | 2019-09-25 | 5 | 5 |
+--------+--------------+--------+-----+
表2:
+-----+--------------+--------+
| idB | history_date | active |
+-----+--------------+--------+
| 1 | 2019-08-01 | true |
+-----+--------------+--------+
| 1 | 2019-09-03 | false |
+-----+--------------+--------+
| 5 | 2019-09-01 | true |
+-----+--------------+--------+
| 5 | 2019-09-25 | false |
+-----+--------------+--------+
例如,我需要选择2019-09-16和2019-09-23或之前最接近的记录。
对于seq_date = 2019-09-16的ex idA = 123456,我将记录2019-09-15
对于seq_date = 2019-09-16的前idB = 1,我将记录为2019-09-03。
然后我将这两个记录合并以获得IDA 2019-09-16的完整记录
对于表1,第一步的结果将是:
+--------+--------------+--------+-----+------------+
| idA | history_date | status | idB | seq_date |
+--------+--------------+--------+-----+------------+
| 123456 | 2019-09-15 | 1 | 1 | 2019-09-16 |
+--------+--------------+--------+-----+------------+
| 123456 | 2019-09-17 | 2 | 1 | 2019-09-23 |
+--------+--------------+--------+-----+------------+
| 654321 | 2019-09-01 | 3 | 5 | 2019-09-16 |
+--------+--------------+--------+-----+------------+
| 654321 | 2019-09-01 | 3 | 5 | 2019-09-23 |
+--------+--------------+--------+-----+------------+
对于表2,第一步的结果将是:
+-----+--------------+--------+------------+
| idB | history_date | active | seq_date |
+-----+--------------+--------+------------+
| 1 | 2019-09-03 | false | 2019-09-16 |
+-----+--------------+--------+------------+
| 1 | 2019-09-03 | false | 2019-09-23 |
+-----+--------------+--------+------------+
| 5 | 2019-09-01 | true | 2019-09-16 |
+-----+--------------+--------+------------+
| 5 | 2019-09-01 | true | 2019-09-23 |
+-----+--------------+--------+------------+
我的预期结果是这样的:(于seq_date加入)
+--------+------------+--------+-----+--------+
| idA | seq_date | status | idB | active |
+--------+------------+--------+-----+--------+
| 123456 | 2019-09-16 | 1 | 1 | false |
+--------+------------+--------+-----+--------+
| 123456 | 2019-09-23 | 2 | 1 | false |
+--------+------------+--------+-----+--------+
| 654321 | 2019-09-16 | 3 | 5 | true |
+--------+------------+--------+-----+--------+
| 654321 | 2019-09-23 | 3 | 5 | true |
+--------+------------+--------+-----+--------+