我有兩個類似的表格,我想根據它們的手提包和它們的內容(結合日期,假設唯一性)加入它們。totearrivalTimestamp 可用于標識表中的唯一手提箱。內容的順序不同(需要以某種方式排序),然后在加入之前按手提袋和日期分組,但我正在努力如何最好地做到這一點。
第一個表看起來像:
Date TotearrivalTimestamp Tote content location
2021-01-01 13:00 A hat 1 <-- first tote arriving at 13:00
2021-01-01 13:00 A cat 1 <-- first tote arriving at 13:00
2021-01-01 14:00 B toy 1 <-- second tote arriving at 14:00
2021-01-01 14:00 B cat 1 <-- second tote arriving at 14:00
2021-01-01 15:00 A toy 1 <-- third tote (reused) arriving at 15:00
2021-01-01 13:00 A toy 1
2021-01-02 13:00 A hat 1
2021-01-02 13:00 A cat 1
第二個表看起來像:
Date ToteendingTimestamp Tote content location
2021-01-01 20:00 B cat 2 <-- second tote ending at 20:00
2021-01-01 19:00 A cat 1 <-- first tote ending at 19:00
2021-01-01 19:00 A hat 1 <-- first tote ending at 19:00
2021-01-01 20:00 B toy 2 <-- second tote ending at 20:00
2021-01-01 14:00 A toy 1
2021-01-02 14:00 A hat 1
2021-01-02 14:00 A cat 1
2021-01-01 16:00 A toy 1 <-- third tote (reused) ending at 16:00
我想要的結果是:
Date Tote TotearrivalTimestamp ToteendingTimestamp location_first_table location_second_table
2021-01-01 A 13:00 19:00 1 1
2021-01-01 B 14:00 20:00 1 1
2021-01-01 A 15:00 16:00 1 2
2021-01-02 A 13:00 14:00 1 1
有超過一百萬行(但如果有日期和內容,可以安全地假設手提包的唯一性,因為實際上有超過 300.000 的內容)。
所以我想加入托特。例如:第一個托特包將使用“2021-01-01”(日期)、“帽子”(內容)、“貓”(內容)和“A”(托特包)連接。
uj5u.com熱心網友回復:
select first.date as Date, first.tote, first.totearrivaltimestamp, second.toteendingtimestamp, first.location as location_first_table, second.location as location_second_table
from first inner join second on first.tote = second.tote and first.content = second.content
uj5u.com熱心網友回復:
select f.date
,f.tote
, f.totearrivaltimestamp
, s.toteendingtimestamp
, f.location as location_first_table
, s.location as location_second_table
from first f
,INNER JOIN "second" s on f.date = s.date
and f.tote = s.tote
and f.content = s.content
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327550.html
