1

I have a database table 'table1' as follows: f_key | begin | counts| 1 | 2018-10-04 | 15 | 1 | 2018-10-06 | 20 | 1 | 2018-10-08 | 34 | 1 | 2018-10-09 | 56 |

I have another database table 'table2' as follows: f_key | p_time | percent| 1 | 2018-10-05 | 80 | 1 | 2018-10-07 | 90 | 1 | 2018-10-08 | 70 | 1 | 2018-10-10 | 60 |

The tables can be joined by the f_key field.

I want to get a combined table as shown below:

If the begin time is earlier than any of the p_time then the p_time value in the combined table would be the same as begin time and the percent value would be 50. (As shown in row 1 in the following table)

If the begin time is later than any of the p_time then the p_time value in the combined table would be the very next available p_time and the percent value would be the corresponding value of the selected p_time. (As shown in row 2, 3 and 4 in the following table)

row | f_key | begin | counts| p_time | percent| 1 | 1 | 2018-10-04 | 15 | 2018-10-04 | 50 | 2 | 1 | 2018-10-06 | 20 | 2018-10-05 | 80 | 3 | 1 | 2018-10-08 | 34 | 2018-10-07 | 90 | 4 | 1 | 2018-10-09 | 56 | 2018-10-08 | 70 |

3
  • Why the first-row percent is 50 instead of 15? Commented Feb 19, 2019 at 2:26
  • As I mentioned in the post if the begin time is earlier than any of the p_time then the p_time value in the combined table would be the same as the begin time and the percent value would be 50. Commented Feb 19, 2019 at 2:39
  • Ok I see I miss the logic I write an answer hope can help you Commented Feb 19, 2019 at 2:47

1 Answer 1

2

You can try to use row_number window function to make row number which is the closest row from table1 by begin.

then use coalesce function to let begin time is earlier than any of the p_time then the p_time value in the combined table would be the same as begin time and the percent value would be 50

PostgreSQL 9.6 Schema Setup:

CREATE TABLE table1(
  f_key INT,
  begin DATE,
  counts INT
);

INSERT INTO table1 VALUES (1,'2018-10-04',15);
INSERT INTO table1 VALUES (1,'2018-10-06',20);
INSERT INTO table1 VALUES (1,'2018-10-08',34);
INSERT INTO table1 VALUES (1,'2018-10-09',56);

CREATE TABLE table2(
  f_key INT,
  p_time DATE,
  percent INT
);


INSERT INTO table2 VALUES (1, '2018-10-05',80);
INSERT INTO table2 VALUES (1, '2018-10-07',90);
INSERT INTO table2 VALUES (1, '2018-10-08',70);
INSERT INTO table2 VALUES (1, '2018-10-10',60);

Query 1:

SELECT ROW_NUMBER() OVER(ORDER BY begin) "row",
       t1.f_key,
       t1.counts,
       coalesce(t1.p_time,t1.begin) p_time,
       coalesce(t1.percent,50) percent
FROM (
   SELECT ROW_NUMBER() OVER(PARTITION BY t1.begin,t1.f_key order by t2.p_time desc) rn,
       t2.p_time,
       t2.percent,
       t1.counts,
       t1.f_key,
       t1.begin
   FROM table1 t1 
   LEFT JOIN table2 t2 ON t1.f_key = t2.f_key and t1.begin > t2.p_time
)t1
WHERE rn = 1

Results:

| row | f_key | counts |     p_time | percent |
|-----|-------|--------|------------|---------|
|   1 |     1 |     15 | 2018-10-04 |      50 |
|   2 |     1 |     20 | 2018-10-05 |      80 |
|   3 |     1 |     34 | 2018-10-07 |      90 |
|   4 |     1 |     56 | 2018-10-08 |      70 |
Sign up to request clarification or add additional context in comments.

1 Comment

I had to add t1.begin in the final query. Other than that, it works like a charm! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.