具有两个表和同一项目的多个实例的最小日期
MIN DATE with Two Tables and multiple instances of same item
我有两个table; Items
和 Items_history
我正在尝试加入。
- 第一个 table (
Items
):有一个带有当前日期的项目键实例
- 第二个 table (
Items_history
):具有多个具有多个日期的相同项目键的实例。只希望第一个 table 的项目键具有第二个 table 的最小日期
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;
我有两个table; Items
和 Items_history
我正在尝试加入。
- 第一个 table (
Items
):有一个带有当前日期的项目键实例 - 第二个 table (
Items_history
):具有多个具有多个日期的相同项目键的实例。只希望第一个 table 的项目键具有第二个 table 的最小日期
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;