sql獲取分組後取某欄位最大一條記錄

2021-08-14 13:12:38 字數 1375 閱讀 7722

1.得到所有xh的最大gotime(去重)

select

xh,max(gotime) as gy

from

group by xh

因為我們還需要得到a.backtime,所以要用inner join內連線得到

select

a.xh as axh,a.gotime as gotime,a.backtime as backtime

from

inner join (

select

xh,max(gotime) as gy

from

group by xh

) y

on a.xh=y.xh and a.gotime=y.gy

2.得到某個xh最大的時間gotime:

select

xh,max(gotime) as gy

from

當需要得到所有xh的最大時間gotime時(去重):

select

a.xh as axh,a.gotime as gotime,a.backtime as backtime

where gotime = (

select

max(b.gotime)

where a.xh = b.xh

)

SQL表分組後獲取最大值的整條記錄

今天在工作的時候需要使用到乙個分組最大值查詢問題,蒐羅了一下答案,了解了一些,為了鞏固知識,還是做一下測試為好 目標 根據course分組求出score最大值的整條記錄 例子 查詢乙個班級中各科目的第一名的整行記錄 建立測試表 create table students course varchar...

SQL 分組後獲取其中乙個字段最大值的整條記錄

有id,name,createdate的一張表testtable 根據name分組,獲取每組中createdate最大的那條記錄 整條 查詢出來 建立一張表,語句如下 sql view plain copy create table dbo testtable id int not null ide...

SQL分組排序後取每組最新一條資料的另一種思路

在hibernate框架和mysql oracle兩種資料庫相容的專案中實現查詢每個id最新更新的一條資料。之前工作中一直用的mybatis oracle資料庫這種,一般寫這類分組排序取每組最新一條資料的sql都是使用row number over 函式來實現 例如 select t1.from s...