对于分组在一个团队中的用户,当我们按日期排序时如何获得前 1 行

How to get the top 1 row when we order by date, for users grouped in one team

我有以下table

Id SubmittedUserId DateSubmitted TeamId PrivateScore PublicScore
2180 647.0 2010-04-29 22:32:08 496 56.21390151977539 55.76919937133789
2181 619.0 2010-04-30 09:38:29 497 50.0 47.11539840698242
2182 619.0 2010-04-30 09:48:50 497 65.60690307617188 61.057701110839844
2184 663.0 2010-05-01 11:02:52 499 50.0 47.11539840698242
2185 673.0 2010-05-02 08:04:38 500 62.283199310302734 61.057701110839844
2186 673.0 2010-05-02 08:13:43 500 62.86130142211914 60.576900482177734
2187 673.0 2010-05-02 08:25:09 500 65.75140380859375 62.5
2188 435.0 2010-05-03 17:48:39 501 50.0 47.11539840698242
2191 435.0 2010-05-04 15:39:56 501 56.502899169921875 55.28850173950195
2192 647.0 2010-05-04 18:56:15 496 56.3583984375 57.21149826049805
2193 652.0 2010-05-04 21:29:39 502 56.791900634765625 56.25
2194 435.0 2010-05-04 22:13:17 501 55.635799407958984 55.76919937133789
2195 652.0 2010-05-04 22:54:07 502 56.64739990234375 53.84619903564453
2196 652.0 2010-05-04 23:46:06 502 55.78030014038086 52.40380096435547
2197 435.0 2010-05-05 00:32:56 501 48.84389877319336 45.192298889160156
2198 435.0 2010-05-05 01:19:13 501 56.069400787353516 55.28850173950195

并且我想编写查询以获取以下内容 table:

Id SubmittedUserId DateSubmitted TeamId PrivateScore PublicScore totalsubmissions     
2180 647         5/4/2010 18:56 496     56.35839844 57.21149826 2     
2182 619         4/30/2010 9:48 497     65.60690308 61.05770111 2     
2184 663      5/1/2010 11:02 499     50         47.11539841 1     
2187 673         5/2/2010 08:25 500     65.75140380 62.5     4     
2198 435         5/5/2010 1:19 501   56.06940079 55.28850174 5     

我写了以下错误的查询。不知道怎么写。

select COUNT(*), 
top 1 Id, SubmittedUserId, DateSubmitted, TeamId, PublicScore, PrivateScore
from [Kaggle].[dbo].[Submissions]
order by DateSubmitted desc 
Group by  TeamId

如有任何帮助,我将不胜感激。

WITH cte as (
    select *,
           row_number() over (partition by TeamId
                              order by DateSubmitted ) as rn,
           count(*) over (partition by TeamId) as total_submission
    from [Kaggle].[dbo].[Submissions]
)
SELECT *
FROM cte
WHERE rn = 1