I need to add value but only once by matching value as can't use a group by because I need to keep category info in my table.
Here is my two tobles.
Table 1
REGION | PERIOD | some value | category |
---|---|---|---|
region1 | 1 | 20 | a |
region1 | 1 | 10 | b |
region2 | 1 | 30 | c |
region2 | 2 | 30 | d |
region3 | 2 | 50 | a |
I want to join on region and period but only with the occurence of the matching.
Table 2
REGION | PERIOD | other value |
---|---|---|
region1 | 1 | 4 |
region2 | 1 | 5 |
region3 | 2 | 6 |
I'm trying to obtain this result:
Result
REGION | PERIOD | some value | category | other value |
---|---|---|---|---|
region1 | 1 | 20 | a | 4 |
region1 | 1 | 10 | b | |
region2 | 1 | 30 | c | 5 |
region2 | 2 | 30 | d | |
region3 | 2 | 50 | e | 6 |
To recreate my dataset sample :
CREATE TABLE table_1 (
region VARCHAR(255),
period VARCHAR(255),
categories VARCHAR(255),
some_value INT
)
INSERT INTO table_name (region, period, categories, int_values)
VALUES ('region1', '1', 'a', 20),
('region1', '1', 'b', 10),
('region2', '1', 'c', 30),
('region2', '2', 'd', 30),
('region3', '2', 'a', 40);
CREATE TABLE table_2 (
region VARCHAR(255),
period VARCHAR(255),
categories VARCHAR(255),
other_values INT
)
INSERT INTO table_name (region, period, int_values)
VALUES ('region1', '1', 4),
('region2', '1', 5),
('region3', '2', 6),