如何获取lastmodifier的最早日期?

时间:2019-05-02 03:02:23

标签: oracle powerbi

我选择获取最早的日期以及另一列的最后一行的同一行。是否有可能像vlookup一样获得相同的行数据。当我MIN(DATE)并将查找另一列相同行的值时。

我尝试获取min(date)和按lastmodifier分组。但是,我不知道脚本选择了哪个lastmodifier。

(1 % 2) % (3 % 4)

我想在每个订单号中选择第一个lastmodifier。

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确,样本数据和预期输出将对此类问题非常有帮助。但我认为您需要这样做:

select substr(code, 1, 2) ordbatch, substr(code, 1, 1) ordernum, min(lstupdtime) earliest, 
       min(lstmodifier) keep (dense_rank first order by lstupdtime) lstmodifier,
       count(case state when 'REVIEWED' then 1 end) reviewed,
       count(case state when 'WAREHOUSE RECEIVE' then 1 end) warehouse
  from casecode 
  where state in ('REVIEWED', 'WAREHOUSE RECEIVE')
  group by substr(code,1,2), substr(code,1,1)

首先:您不需要union,将条件countcase一起使用。但是,您的主要问题的答案是min ... keep ... first。它找到某一列(lstmodifier)的值,而另一列(lstupdtime)的值最低。 min表示,如果两个运算符满足条件,我们必须选择一个人,因此我们按字母顺序优先。我不知道您要在这种情况下做什么,具体取决于使用不同的解决方案,例如ranklistagg

这里是示例:

with casecode(code, state, lstupdtime, lstmodifier) as (
    select 'A1', 'REVIEWED',          date '2018-12-25', 'Clark' from dual union all
    select 'A1', 'OTHER',             date '2018-12-25', 'Clark' from dual union all
    select 'A1', 'WAREHOUSE RECEIVE', date '2018-12-25', 'Clark' from dual union all
    select 'A1', 'WAREHOUSE RECEIVE', date '2018-12-24', 'Jones' from dual union all
    select 'B1', 'WAREHOUSE RECEIVE', date '2018-12-25', 'Clark' from dual union all
    select 'B2', 'WAREHOUSE RECEIVE', date '2018-12-25', 'Clark' from dual 
  )
select substr(code, 1, 2) ordbatch, substr(code, 1, 1) ordernum, min(lstupdtime) earliest, 
       min(lstmodifier) keep (dense_rank first order by lstupdtime) lstmodifier,
       count(case state when 'REVIEWED' then 1 end) reviewed,
       count(case state when 'WAREHOUSE RECEIVE' then 1 end) warehouse
  from casecode 
  where state in ('REVIEWED', 'WAREHOUSE RECEIVE')
  group by substr(code,1,2), substr(code,1,1)

我使用较短的substr来进行code的操作,以使情况更清楚,请改用14和15。琼斯之所以被选中,是因为他的lstupdtime是最低的:

ORDBATCH ORDERNUM EARLIEST    LSTMODIFIER   REVIEWED  WAREHOUSE
-------- -------- ----------- ----------- ---------- ----------
A1       A        2018-12-24  Jones                1          2
B1       B        2018-12-25  Clark                0          1
B2       B        2018-12-25  Clark                0          1