[英]How to add another result of query that result i made to a result (SQL)
[英]How would I exclude query results if the result has a timestamp within a particular range of another result?
假設我有以下結果
Person Action Item Time
Jane Ate Apple 2015-10-27 18:52:22
Jane Ate Apple 2015-10-27 18:53:18
Jane Ate Apple 2015-10-28 5:16:45
我希望合並前兩個Jane結果,並且只出現一次,因為它們發生在同一天……本質上是在尋找每天的最大時間,而不是通常的最大時間。
使用row_number
選擇每天每人每個項目的操作以及最長時間:
select * from (
select row_number() over (partition by person, action, item, CONVERT(date, time) order by time desc) rn, *
from mytable
) t1 where rn = 1
我認為您想group by
並使用max
作為匯總函數。 像這樣:
select person, action, item, max(time)
from mytable
group by person, action, item, convert(date, time)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.