【问题标题】:T-SQL get the last date time recordT-SQL 获取最后的日期时间记录
【发布时间】:2018-04-09 09:29:37
【问题描述】:

我的桌子是这样的:

+---------+------------------------+-------+---------+---------+
|channel  |date                    |code   |comment  |order_id |
+---------+------------------------+-------+---------+---------+
|1        |2017-10-27 12:04:45.397 |2      |comm1    |1        |
|1        |2017-10-27 12:14:20.997 |1      |comm2    |1        |
|2        |2017-10-27 12:20:59.407 |3      |comm3    |1        |
|2        |2017-10-27 13:14:20.997 |1      |comm4    |1        |
|3        |2017-10-27 12:20:59.407 |2      |comm5    |1        |
|3        |2017-10-27 14:20:59.407 |1      |comm6    |1        |
+---------+------------------------+-------+---------+---------+

我希望得到这样的结果:

+---------+------------------------+-------+---------+
|channel  |date                    |code   |comment  |
+---------+------------------------+-------+---------+
|1        |2017-10-27 12:14:20.997 |1      |comm2    |
|2        |2017-10-27 13:14:20.997 |1      |comm4    |
|3        |2017-10-27 14:20:59.407 |1      |comm6    |
+---------+------------------------+-------+---------+

每个频道总是有 1 条记录,order_id = x 和最大日期。通道总数是恒定的。 我的查询有效,但随着表的增长,我担心性能。执行三个几乎相同的查询似乎并不聪明。

select
    *
from
    (select top(1)
        channel,
        date,
        code,
        comment
    from
        status
    where
        channel = 1 and
        order_id = 1 and
        cast(date as date) = '2017-10-27'
    order by 
        date desc) channel1
union
select 
    *
from
    (select top(1)
        channel,
        date,
        code,
        comment
    from
        status
    where
        channel = 2 and
        order_id = 1 and
        cast(date as date) = '2017-10-27'
    order by 
        date desc) channel2
union
select 
    *
from
    (select top(1)
        channel,
        date,
        code,
        comment
    from
        status
    where
        channel = 3 and
        order_id = 1 and
        cast(date as date) = '2017-10-27'
    order by 
        date desc) channel3

我该如何改进?

【问题讨论】:

标签: sql database tsql sql-server-2014


【解决方案1】:

另一个选项是使用 WITH TIES 子句。没有子查询或额外的字段。

Select top 1 with ties *
 From  YourTable
 Order By Row_Number() over (Partition By channel order by date desc)

【讨论】:

  • 不知道你能做到这一点。太棒了,谢谢分享。
  • @Simon 我每天都在这里学到一些东西。这就是有趣的部分:)
  • 非常优雅的解决方案!我要用这个。谢谢你,先生。 :)
  • @gutowskiap 很高兴它有帮助
【解决方案2】:

尝试使用ROW_NUMBER() 函数和派生表。它将为您省去很多麻烦。试试:

select channel
       ,date
       ,code
       ,comment
from
(select *
       ,row_number() over(partition by channel order by code asc) rn --probably don't need asc since it is ascending by default
from mytable) t
where t.rn = 1

【讨论】:

    【解决方案3】:

    假设您想要每个频道的最新行,这将起作用。

    SELECT *
    FROM (
        SELECT
            ROW_NUMBER() OVER (PARTITION BY s.channel ORDER BY [date] DESC) AS rn,
            *
        FROM [status] AS s
    ) AS t
    WHERE t.rn = 1
    

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 1970-01-01
      • 2011-04-09
      • 2021-11-19
      • 2011-05-22
      • 2014-03-25
      • 2021-04-17
      • 2015-05-19
      • 2014-12-30
      相关资源
      最近更新 更多