SQL分组取每组前一(或几)条记录(排名)

USE [Test]
GO
/****** Object:  Table [dbo].[scores]    Script Date: 2018/3/11 17:59:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[scores](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](10) NULL,
    [course] [varchar](10) NULL,
    [score] [decimal](4, 2) NULL,
 CONSTRAINT [PK__score__3214EC276D139B95] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[scores] ON 

INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (1, N'小明', N'语文', CAST(90.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (2, N'小明', N'数学', CAST(80.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (3, N'小明', N'英语', CAST(60.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (4, N'小红', N'语文', CAST(93.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (5, N'小红', N'数学', CAST(92.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (6, N'小红', N'英语', CAST(91.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (7, N'小花', N'语文', CAST(50.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (8, N'小花', N'数学', CAST(30.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (9, N'小花', N'英语', CAST(70.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (10, N'小草', N'语文', CAST(95.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (11, N'小草', N'数学', CAST(86.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (12, N'小草', N'英语', CAST(62.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (13, N'小刚', N'语文', CAST(78.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (14, N'小刚', N'数学', CAST(68.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (15, N'小刚', N'英语', CAST(76.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (16, N'小柔', N'语文', CAST(56.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (17, N'小柔', N'数学', CAST(86.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (18, N'小柔', N'英语', CAST(88.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (19, N'小陈', N'语文', CAST(77.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (20, N'小陈', N'数学', CAST(88.00 AS Decimal(4, 2)))
INSERT [dbo].[scores] ([ID], [name], [course], [score]) VALUES (21, N'小陈', N'英语', CAST(99.00 AS Decimal(4, 2)))
SET IDENTITY_INSERT [dbo].[scores] OFF

方法一:

select  course,name, score from scores sc where name in(
select top 3 name from scores  where sc.course=course
)
group by  course,name, score order by course,score desc

方法二:


select  * from (
    select *, ROW_NUMBER() over(partition by course order by score desc) rowNum
    from scores 
) as s where s.rowNum<=3
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hyunbar/article/details/79518494
文章标签: sql
个人分类: SQL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭