SQL group by 分组后,同一组的排序后取第一条
Posted John_杰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL group by 分组后,同一组的排序后取第一条相关的知识,希望对你有一定的参考价值。
SELECT * FROM(
SELECT
[SPID]
,[PH1]
,[PH1_Code]
,[ProjectName]
,ROW_NUMBER() OVER(PARTITION BY [SPID] ORDER BY [SPID]) RowNum
FROM(
SELECT
a.[SPID]
,b.[ProjectName]
,b.[PH1] AS [PH1_Code]
,e.[Description] AS [PH1]
FROM [dbo].[CAE_SPAndTask] a
LEFT JOIN [dbo].[CAE_Task] b ON b.[CAENumber]= a.[CAENumber]
LEFT JOIN [dbo].[CAE_PH1PH2] e ON e.[ProductHierarchy] = b.[PH1]
)t4
GROUP BY [SPID],[ProjectName],[PH1_Code],[PH1]
)t5
WHERE RowNum = 1
以上是关于SQL group by 分组后,同一组的排序后取第一条的主要内容,如果未能解决你的问题,请参考以下文章
MSSQL 分组后取每组第一条(group by order by)