如何返回当天写的最新一行?

时间:2009-02-10 19:45:18

标签: sql sql-server

我有一个包含FooId和CreatedTime列的表。在白天,可以为同一个FooId插入多行。 CreatedTime列表示插入时的时间。

我想要一个查询,它会返回给定日期的最新行(例如2000-01-01)。有没有办法编写一个可以用SQL Server 2005执行此操作的查询?

下面是我期待的数据和结果的示例。 我想要当天创建的最新数据。所以MAX(Cre​​atedDate)无效。谢谢!

FooId   Data  CreatedTime
---------------------------
1       A     2000/01/01 12:00:00
1       B     2000/01/01 12:12:00
1       C     2000/01/01 12:25:00
2       A     2000/01/01 12:00:00
2       B     2000/01/01 12:26:00
3       A     2000/01/01 12:00:00

结果

FooId   Data  CreatedTime
---------------------------
1       C     2000/01/01 12:25:00
2       B     2000/01/01 12:26:00
3       A     2000/01/01 12:00:00

9 个答案:

答案 0 :(得分:4)

当然它适用于MAX

因为你没有提供DDL和DML语句,所以我使用了我已经存在的东西

运行此

    CREATE TABLE #MaxVal(id INT,VALUE varchar(10),SomeDate DATETIME)
    INSERT #MaxVal VALUES(1,'a','2009-02-10 14:48:45.143')
    INSERT #MaxVal VALUES(1,'b','2009-02-10 13:48:45.143')
    INSERT #MaxVal VALUES(1,'c','2009-02-10 11:48:45.143')
    INSERT #MaxVal VALUES(2,'d','2009-02-10 11:48:45.143')
    INSERT #MaxVal VALUES(2,'e','2009-02-10 12:48:45.143')
    INSERT #MaxVal VALUES(2,'f','2009-02-10 13:48:45.143')
    INSERT #MaxVal VALUES(3,'g','2009-02-10 11:48:45.143')
    INSERT #MaxVal VALUES(3,'h','2009-02-10 14:48:45.143')




SELECT t.* FROM(
    SELECT id,MAX(SomeDate) AS MaxValue
    FROM #MaxVal
    WHERE SomeDate >='2009-02-10'
        AND SomeDate < '2009-02-11'
    GROUP BY id) x
    JOIN #MaxVal t ON x.id =t.id
    AND x.MaxValue =t.SomeDate

输出

id  VALUE   SomeDate
3   h   2009-02-10 14:48:45.143
2   f   2009-02-10 13:48:45.143
1   a   2009-02-10 14:48:45.143

答案 1 :(得分:1)

因为要求发生变化而删除旧帖子并不警告是不是很粗鲁?难道你不能关闭它,所以人们不会想知道发生了什么?

无论如何,这里是更新后的答案:

SELECT Foo.* 
FROM Foo
JOIN (
  SELECT FooId, MAX(CreatedTime) 
  FROM Foo Q
  -- Only change the dates in the next line.
  WHERE Q.CreatedTime >= '20000101' AND Q.CreatedTime < '20000102'
  GROUP BY Q.FooId, DATEADD(day, DATEDIFF(day, '19000101', Q.CreatedTime), '19000101')
) Q2 (FooID, CreatedTime) ON Q2.FooID = Foo.FooID AND Q2.CreatedTime = Foo.CreatedTime
ORDER BY FooID

<强>结果

FooId   Data    CreatedTime
1   C   2000-01-02 12:25:00.000
2   B   2000-01-02 12:26:00.000
3   A   2000-01-02 12:00:00.000

<强> DDL

CREATE TABLE Foo (FooId int NOT NULL, Data varchar(10), CreatedTime datetime NOT NULL)

INSERT INTO Foo VALUES (1, 'A', '2000-01-01 12:00:00')
INSERT INTO Foo VALUES (1, 'B', '2000-01-01 12:12:00')
INSERT INTO Foo VALUES (1, 'C', '2000-01-01 12:25:00')
INSERT INTO Foo VALUES (2, 'A', '2000-01-01 12:00:00')
INSERT INTO Foo VALUES (2, 'B', '2000-01-01 12:26:00')
INSERT INTO Foo VALUES (3, 'A', '2000-01-01 12:00:00')

INSERT INTO Foo VALUES (1, 'A', '2000-01-02 12:00:00')
INSERT INTO Foo VALUES (1, 'B', '2000-01-02 12:12:00')
INSERT INTO Foo VALUES (1, 'C', '2000-01-02 12:25:00')
INSERT INTO Foo VALUES (2, 'A', '2000-01-02 12:00:00')
INSERT INTO Foo VALUES (2, 'B', '2000-01-02 12:26:00')
INSERT INTO Foo VALUES (3, 'A', '2000-01-02 12:00:00')

答案 2 :(得分:1)

所有的日子,试试这个:

SELECT FooId, Max(CreatedTime)
FROM Foo
GROUP BY FooId, cast(CreatedTime as int)

对于某一天,请执行以下操作:

SELECT FooId, Max(CreatedTime)
FROM Foo
WHERE CreatedTime >= '2000-01-01' and CreatedTime < '2000-01-02'
GROUP BY FooId, cast(CreatedTime as int)

答案 3 :(得分:0)

您可能希望拥有一个仅存储日期的计算列或其他列。在此之后,查询将是微不足道的。

答案 4 :(得分:0)

这会在SqlServer2005中运行吗?

SELECT FoodId, Data, CreatedTime WHERE CreatedTime=max(CreatedTime) GROUP BY FoodId, day(CreatedTime)

答案 5 :(得分:0)

select data 
 from fooTable 
 where CreatedTime between <beginning of day> and <end of day> 
 order by CreatedTime DESC 
 limit 1

也许是这样的事情?没有经过测试,脱离了我的脑海。

答案 6 :(得分:0)

试试这个......

select f1.FooID, f1.Data, f1.CreatedTime
from Foo f1
inner join
(
    select f2.FooID, max(f2.CreatedTime) as CreatedTime
    from Foo f2
    where f2.CreatedTime >= '2000-01-01' and f2.CreatedTime < '2000-01-02'
    group by f2.FooID
) f3
on f3.FooID = f1.FooID
and f3.CreatedTime = f1.CreatedTime
order by 1, 2, 3

答案 7 :(得分:0)

选择顶部1 *
来自 foo
其中 @today和@tomorrow之间的CreatedTime 订购 CreatedTime desc

(@ today和@tomorrow是您想要的日期范围的占位符。)

答案 8 :(得分:0)

select x.FooId, x.Data, x.CreatedTime
from (select FooId,
             Data,
             CreatedTime,
             row_number() over (partition by FooId
                                order by CreatedTime desc) as rn
       from Foo
       where CreatedTime >= ...
          and CreatedTime < ...) as x
where x.rn = 1
order by FooId