具有两个表和同一项目的多个实例的最小日期

MIN DATE with Two Tables and multiple instances of same item

我有两个table; ItemsItems_history 我正在尝试加入。


Items Table:                   Items_History table:        Results:
item_key    date               item_key       date          item_key   date
1          1/1/2019              1          1/1/2019          1       8/30/2018
2          12/30/2018            1          10/30/2018        2       10/15/2018
                                 1          08/30/2018
                                 2          12/30/2018
                                 2          10/15/2018

我目前拥有的:

select c.item_key,min(a.CREATE_TMSTMP) 
from wf_items_history a
inner join wf_items c on c.item_key = a.item_key and c.form_number = 'MV1' and c.ASSIGNED_WORKGROUP = 'NONV'
group by c. item_key, a.create_tmstmp

您的查询基本正确,您应该从 group by 子句中排除 tmstmp 列。

select c.item_key,min(a.CREATE_TMSTMP) 
from wf_items_history a
inner join wf_items c on c.item_key = a.item_key and c.form_number = 'MV1' and c.ASSIGNED_WORKGROUP = 'NONV'
group by c. item_key;