如何为每个SKU返回最新的LogTime

时间:2019-07-11 16:17:26

标签: sql sql-server sql-server-2014

这是我当前查询所产生的:

EventID Sku         User1           LogTime
3510    02821-99-0  Item Inducted   2019-07-08
3510    02821-99-0  Item Inducted   2019-07-06
3510    12573-88-L  Item Inducted   2019-07-08
3510    12573-88-L  Item Inducted   2019-07-07
3510    12948-96-M  Item Inducted   2019-07-06
3510    12948-96-M  Item Inducted   2019-07-05
3510    12948-96-M  Item Inducted   2019-07-05

我想要的是:(每个SKU的最新事件ID 3510)

EventID Sku         User1           LogTime
3510    02821-99-0  Item Inducted   2019-07-08
3510    12573-88-L  Item Inducted   2019-07-08
3510    12948-96-M  Item Inducted   2019-07-06

我尝试使用MAX FUNCTION,但查询失败。

以下是我当前正在使用的查询:

select
tl.EventID,
tl.Sku,
tl.User1,
--MAX(tl.LogTime) as 'LogTime'
tl.LogTime

from dmhost.tblTransactionLog tl

where tl.logTime between '7/5/2019' and '7/9/2019'
and tl.sku not like 'NULL'
and tl.sku <> ''
and tl.sku not like 'Unknown'
and tl.EventID like '3510'

--group by tl.Sku,tl.EventID
order by tl.Sku

我评论了我尝试过的内容。 谢谢!

3 个答案:

答案 0 :(得分:2)

这将在SQL Server和MySQL中工作。 (https://www.db-fiddle.com/f/kDhqfwDG2bUcqm5GKUbdj3/0

首先为EventID和Sku的每种组合获取最大LogTime,然后从tblTransactionLog加入该日志。

SELECT     tl.EventID
         , tl.Sku
         , tl.User1
         , tl.LogTime
FROM       dmhost.tblTransactionLog tl
INNER JOIN (
               SELECT EventID
                    , Sku
                    , MAX(LogTime) AS MaxLogTime
               FROM   dmhost.tblTransactionLog
               WHERE  LogTime BETWEEN '7/5/2019' AND '7/9/2019'
           GROUP BY   EventID, Sku
           ) AS tmax ON tl.EventID = tmax.EventID
                        AND tl.Sku = tmax.Sku
                        AND tl.LogTime = tmax.MaxLogTime
WHERE    tl.LogTime BETWEEN '7/5/2019' AND '7/9/2019'

答案 1 :(得分:0)

对于按row_number()EventID列进行分区,以及由于Sku采用最新的LogTime分区,您可以使用窗口分析功能order by LogTime desc:< / p>

select q.EventID, q.Sku, q.User1, q.LogTime
  from
  (
   select tl.EventID, tl.Sku, tl.User1, t1.LogTime
          row_number() over (partition by tl.EventID, t1.Sku order by tl.LogTime desc) as rn
     from dmhost.tblTransactionLog tl    
    where tl.logTime between '2019-07-05' and '2019-07-09'
      and tl.sku not like 'NULL'
      and tl.sku <> ''
      and tl.sku not like 'Unknown'
      and tl.EventID like '3510'
  ) q
where rn = 1
order by q.Sku

答案 2 :(得分:0)

我建议将此查询写为:

select tl.EventID, tl.Sku, tl.User1, tl.LogTime
from (select tl.*,
             row_number() over (partition by tl.EventID, tl.sku order by tl.logTime desc) as seqnum
      from dmhost.tblTransactionLog tl
      where tl.logTime >= '2019-07-05' and
            tl.logtime < '2019-07-09' and
            tl.sku not in ('NULL', '', 'Unknown') and
            tl.EventID like 3510
     ) tl
where seqnum = 1
order by tl.Sku;

注意:

  • 使用正确的日期/时间格式,以便它们无歧义。我更喜欢连字符(ISO 8601标准)。 SQL Server首选不带连字符的值。
  • 您的comparisons on sku is simplified using字符串不可用。
  • EventID看起来像一个数字,所以我假设它是数字,并删除了引号进行比较,并将LIKE更改为=
  • 亚伦·伯特兰德(Aaron Bertrand)很好explanation为何不希望将BETWEEN与日期/时间值一起使用。