0

我试图从 wip_discrete_jobs_v 中选择 *,但也从 wip_move_transactions_v 中为特定的 wip_entity_id 和 organization_id 选择最新的移动行。我的结果只会是每个 wip_entity_id 一行,只有来自 wip_move_transactions_v 视图的最新(最新)行。

我似乎无法弄清楚如何加入或使用内联视图来仅从移动视图中获取最新(最新)移动。

有人可以帮忙处理 SQL。看起来很简单,但我有一个心理障碍。

4

2 回答 2

0

谢谢蓝脚。您的答案是正确的,并且比我提出的解决方案要快。

--bluefeet 的方式,2 秒

select *
from
(select 
     j.wip_entity_id
    ,m.transaction_date
    ,m.to_operation_seq_num
    ,row_number() over(partition by j.wip_entity_id order by m.transaction_date desc) rn
    from wip_discrete_jobs_v j
    inner join wip_move_transactions_v m
        on j.wip_entity_id = m.wip_entity_id 
        and j.organization_id = m.organization_id
        and j.status_type in (3,6)
) d
where 1=1
  and rn = 1
  and to_operation_seq_num = 30
order by d.wip_entity_id desc;

——我的方式,53 秒

select
 j.wip_entity_id
,m.transaction_date
,m.to_operation_seq_num
from wip_discrete_jobs_v j, wip_move_transactions_v m
where 1=1
  and j.status_type in (3,6)
  and m.wip_entity_id = j.wip_entity_id
  and m.to_operation_seq_num = 30
  and m.organization_id = j.organization_id
  and m.transaction_id = (select max(transaction_id) from WIP_MOVE_TRANSACTIONS_v where wip_entity_id = j.wip_entity_id)
order by j.wip_entity_id desc;
于 2013-05-30T13:51:14.507 回答
0

如果没有看到视图中的列,您应该能够使用row_number()为每个返回一行wip_entity_id

select *
from
(
    select * -- replace this with the columns that you want
       , row_number() over(partition by j.wip_entity_id order by m.trans_date desc) rn
    from wip_discrete_jobs_v j
    inner join wip_move_transactions_v m
        on j.wip_entity_id = m.wip_entity_id  -- add the columns you join on 
        and j.organization_id = m.organization_id
) d
where rn = 1
于 2013-05-28T19:47:14.343 回答