2つのテーブルを特定の期間におけるレコードでjoinをしようとしています。
joinするtable_A をevent_codeで連結したいのですが、table_Bのevent_timeより過去日での最新日付を持ったevent_codeでjoinさせたいです。
テーブルの仕様は以下です。
table_A
event_code | reg_time | description |
---|---|---|
apple | 2022-08-01 00:00:00 | it's apple |
orange | 2022-08-01 00:00:00 | it's orange |
grape | 2022-08-01 00:00:00 | it's grape |
apple2 | 2022-09-01 00:00:00 | big apple |
orange | 2022-09-01 00:00:00 | big orage |
grape | 2022-09-01 00:00:00 | big grape |
ice | 2022-10-01 00:00:00 | big ice |
table_B
event_code | event_time | user_id |
---|---|---|
apple | 2022-08-10 00:00:00 | 0001 |
orange | 2022-08-21 00:00:00 | 0001 |
grape | 2022-08-30 00:00:00 | 0001 |
apple2 | 2022-09-11 00:00:00 | 0001 |
orange | 2022-09-21 00:00:00 | 0001 |
grape | 2022-09-31 00:00:00 | 0001 |
apple2 | 2022-10-11 00:00:00 | 0001 |
ice | 2022-10-15 00:00:00 | 0001 |
orange | 2022-10-21 00:00:00 | 0001 |
grape | 2022-10-30 00:00:00 | 0001 |
join後の期待する結果は以下の通りです。
SQLクエリで書けますでしょうか?
event_code | event_time | user_id | reg_time | description |
---|---|---|---|---|
apple | 2022-08-10 00:00:00 | 0001 | 2022-08-01 00:00:00 | it's apple |
orange | 2022-08-21 00:00:00 | 0001 | 2022-08-01 00:00:00 | it's orange |
grape | 2022-08-30 00:00:00 | 0001 | 2022-08-01 00:00:00 | it's grape |
apple2 | 2022-09-11 00:00:00 | 0001 | 2022-09-01 00:00:00 | big apple |
orange | 2022-09-21 00:00:00 | 0001 | 2022-09-01 00:00:00 | big orage |
grape | 2022-09-31 00:00:00 | 0001 | 2022-09-01 00:00:00 | big grape |
apple2 | 2022-10-11 00:00:00 | 0001 | 2022-09-01 00:00:00 | big apple |
ice | 2022-10-15 00:00:00 | 0001 | 2022-10-01 00:00:00 | big ice |
orage | 2022-10-21 00:00:00 | 0001 | 2022-09-01 00:00:00 | big orange |
grape | 2022-10-30 00:00:00 | 0001 | 2022-09-01 00:00:00 | big grape |
テーブル作成のコードは以下の通りです。
CREATE or replace TABLE table_A
(event_code VARCHAR(10),
reg_time DATETIME,
description VARCHAR(15));
INSERT INTO table_A
VALUES
('apple', '2022-08-01 00:00:00', 'it''s apple'),
('orange', '2022-08-01 00:00:00', 'it''s orange'),
('grape', '2022-08-01 00:00:00', 'it''s grape'),
('apple2', '2022-09-01 00:00:00', 'big apple'),
('orange', '2022-09-01 00:00:00', 'big orage'),
('grape', '2022-09-01 00:00:00', 'big grape'),
('ice', '2022-10-01 00:00:00', 'big ice');
CREATE or replace TABLE table_B
(event_code VARCHAR(10),
event_time DATETIME,
user_id VARCHAR(5));
INSERT INTO table_B
VALUES
('apple', '2022-08-10 00:00:00', '0001'),
('orange', '2022-08-21 00:00:00', '0001'),
('grape', '2022-08-30 00:00:00', '0001'),
('apple2', '2022-09-11 00:00:00', '0001'),
('orange', '2022-09-21 00:00:00', '0001'),
('grape', '2022-09-30 00:00:00', '0001'),
('apple2', '2022-10-11 00:00:00', '0001'),
('ice', '2022-10-15 00:00:00', '0001'),
('orange', '2022-10-21 00:00:00', '0001'),
('grape', '2022-10-30 00:00:00', '0001'),
;
sql
タグで十分です。mysql
,postgresql
,sql-server
にはいずれもCREATE or replace TABLE ...
という構文はなさそう?