Given 2 tables:
Table A
|id|datefrom |dateto |value| |--|-------- |------ |-----| |1 |2021-01-01|2021-01-07 | 7 | |2 |2021-01-08|2021-01-15 | 9 | |3 |2021-01-16|2021-01-23 | 4 |
Table B
|id|transdate |value| |--|-------- |-----| |1 |2021-01-03 | 3 | |2 |2021-01-10 | 6 | |3 |2021-01-20 | 3 |
I want to produce the following View:
|id|transdate |B Value | A Value| AxB Value | |--|--------- |--------|--------|-----------| |1 |2021-01-03| 3 | 7 | 21 | |2 |2021-01-10| 6 | 9 | 54 | |3 |2021-01-20| 3 | 4 | 12 |
My Question, how to get Table A Value inside of that view, based on that transaction date within Table A Date From and Date To?
Advertisement
Answer
This query will match each table_a
entry to every table_b
transaction, based only on the transaction date being BETWEEN
datefrom
and dateto
. table_a.id
is ignored.
SELECT table_b.id, table_b.transdate, table_b.value AS "b_value", table_a.value AS "a_value", table_a.value * table_b.value AS "ab_value" FROM table_b LEFT JOIN table_a ON table_b.transdate BETWEEN table_a.datefrom AND table_a.dateto;
Output:
| id | transdate | b_value | a_value | ab_value | | --- | ------------------------ | ------- | ------- | -------- | | 1 | 2021-01-03T00:00:00.000Z | 3 | 7 | 21 | | 2 | 2021-01-10T00:00:00.000Z | 6 | 9 | 54 | | 3 | 2021-01-20T00:00:00.000Z | 3 | 4 | 12 |