简体   繁体   English

SQL:Select 给定日期时间的每一天的最后一条记录

[英]SQL: Select the last record for each day given datetime

I have a table of stock data (the db is MySQL):我有一个stock数据表(数据库是 MySQL):

trade_time          price  
2013-01-02 09:43:00 515.61
2013-01-03 09:39:00 525.90
2013-01-03 11:38:00 539.44
2013-01-03 13:22:00 509.16
2013-01-04 09:47:00 507.40
2013-01-04 14:33:00 517.45
2013-01-07 09:33:00 550.42
2013-01-07 13:13:00 524.85
2013-01-07 14:51:00 536.44

I would like to return the last traded price for each day我想返回每天的最后成交价

trade_date price  
2013-01-02 515.61
2013-01-03 509.16
2013-01-04 517.45
2013-01-07 536.44

What makes this question different from other "selecting the latest record based on datetime" questions on this site is that input is in datetime but output is in date .这个问题与本网站上其他“根据日期时间选择最新记录”问题的不同之处在于输入是在datetime但 output 是在date Let me know this question has already been answered.让我知道这个问题已经得到解答。

You may join to a subquery which finds the maximum datetime for each date.您可以加入一个子查询,该查询可以找到每个日期的最大日期时间。

SELECT t1.trade_time, t1.price
FROM yourTable t1
INNER JOIN
(
    SELECT DATE(trade_time) AS trade_date, MAX(trade_time) AS max_trade_time
    FROM yourTable
    GROUP BY DATE(trade_time)
) t2
    ON t2.trade_date = DATE(t1.trade_time) AND
       t2.max_trade_time = t1.trade_time
ORDER BY
    t1.trade_time;

下面演示的屏幕截图

Demo 演示

Here is an efficient solution using window function ROW_NUMBER() over a type cast of the timestamp column to date:这是一个有效的解决方案,使用 window function ROW_NUMBER()对迄今为止的时间戳列的类型转换:

select trade_date, price
from (
    select trade_date, price, row_number() over
        (partition by date(trade_date) 
         order by trade_date desc) as row_num 
    from stock) as subquery
where row_num = 1
order by trade_date

You can use a correlated subquery:您可以使用相关子查询:

select t.*
from t
where t.trade_date = (select max(t2.trade_date)
                      from t t2
                      where date(t2.trade_date) = date(t.trade_date)
                     );

I understand this is an implementation in MY-SQL , but I tried in SQL-Server just for fun and this query gives the requisite output:我知道这是MY-SQL中的一个实现,但我在SQL-Server中尝试只是为了好玩,这个查询提供了必要的 output:

SELECT CAST(SQ.TT AS DATE) AS TRADED_DATE, PRICE
FROM STOCK
INNER JOIN
(SELECT MAX(TRADE_TIME) AS TT
FROM STOCK
GROUP BY CAST(TRADE_TIME AS DATE)) SQ
ON SQ.TT = TRADE_TIME

Output as Output 为

TRADED_DATE PRICE
----------- ---------------------------------------
2013-01-02  515.61
2013-01-03  509.16
2013-01-04  517.45
2013-01-07  536.44

(4 rows affected)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM