简体   繁体   中英

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. 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...

在此处输入图片说明

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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