简体   繁体   English

SQL-按日期选择日期时间的最大值

[英]SQL - select max of datetime by day

Using the below example, I want to get a count of records grouped by Person and Product, for each day. 使用下面的示例,我想获得每天按“人”和“产品”分组的记录数。

The example on the left is the data in the SQL Server 2008 database. 左侧的示例是SQL Server 2008数据库中的数据。 The data on the right is my desired query result. 右边的数据是我想要的查询结果。

I could just substring off the time portion of the date values, then just do a Group By... but ideally the Max(date) would contain the full timestamp... 我可以从日期值的时间部分中减去子串,然后进行分组依据...但理想情况下,Max(date)应该包含完整的时间戳记...

在此处输入图片说明

Is this what you want? 这是你想要的吗?

select t.person, t.product, count(*) as cnt, max(t.date) as date
from table t
group by t.person, t.product, cast(t.date as date);

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

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