Loading

SQL技巧之排名统计

有一道SQL笔试题是这样子的:

 

已知表信息如下:

Department(depID, depName),depID 系编号,DepName系名

Student(stuID, name, depID) 学生编号,姓名,系编号

Score(stuID, category, score) 学生编码,科目,成绩

找出每一个系的最高分,并且按系编号,学生编号升序排列,要求顺序输出以下信息:

系编号,系名,学生编号,姓名,总分

 

USE [test]
GO
/****** Object:  Table [dbo].[Score]    Script Date: 05/11/2015 23:16:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Score](
    [stuID] [int] NOT NULL,
    [category] [varchar](50) NOT NULL,
    [score] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (1, N'英语', 80)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (2, N'数学', 80)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (1, N'数学', 70)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (2, N'英语', 89)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (3, N'英语', 81)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (3, N'数学', 71)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (4, N'数学', 91)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (4, N'英语', 61)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (5, N'英语', 91)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (6, N'英语', 89)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (7, N'英语', 77)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (8, N'英语', 97)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (9, N'英语', 57)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (5, N'数学', 87)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (6, N'数学', 89)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (7, N'数学', 80)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (8, N'数学', 81)
INSERT [dbo].[Score] ([stuID], [category], [score]) VALUES (9, N'数学', 84)
/****** Object:  Table [dbo].[Department]    Script Date: 05/11/2015 23:16:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Department](
    [depID] [int] IDENTITY(1,1) NOT NULL,
    [depName] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [depID] 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].[Department] ON
INSERT [dbo].[Department] ([depID], [depName]) VALUES (1, N'计算机')
INSERT [dbo].[Department] ([depID], [depName]) VALUES (2, N'生物')
INSERT [dbo].[Department] ([depID], [depName]) VALUES (3, N'数学')
SET IDENTITY_INSERT [dbo].[Department] OFF
/****** Object:  Table [dbo].[Student]    Script Date: 05/11/2015 23:16:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
    [stuID] [int] IDENTITY(1,1) NOT NULL,
    [stuName] [varchar](50) NOT NULL,
    [deptID] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [stuID] 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].[Student] ON
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (1, N'计算机张三', 1)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (2, N'计算机李四', 1)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (3, N'计算机王五', 1)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (4, N'生物amy', 2)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (5, N'生物kity', 2)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (6, N'生物lucky', 2)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (7, N'数学_yiming', 3)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (8, N'数学_haoxue', 3)
INSERT [dbo].[Student] ([stuID], [stuName], [deptID]) VALUES (9, N'数学_wuyong', 3)
SET IDENTITY_INSERT [dbo].[Student] OFF
/****** Object:  Default [DF__Departmen__depNa__5441852A]    Script Date: 05/11/2015 23:16:23 ******/
ALTER TABLE [dbo].[Department] ADD  DEFAULT ('') FOR [depName]
GO
/****** Object:  Default [DF__Score__category__5EBF139D]    Script Date: 05/11/2015 23:16:23 ******/
ALTER TABLE [dbo].[Score] ADD  DEFAULT ('') FOR [category]
GO
/****** Object:  Default [DF__Score__score__5FB337D6]    Script Date: 05/11/2015 23:16:23 ******/
ALTER TABLE [dbo].[Score] ADD  DEFAULT ((0)) FOR [score]
GO
/****** Object:  Default [DF__Student__stuName__59063A47]    Script Date: 05/11/2015 23:16:23 ******/
ALTER TABLE [dbo].[Student] ADD  DEFAULT ('') FOR [stuName]
GO
/****** Object:  ForeignKey [FK__Student__deptID__59FA5E80]    Script Date: 05/11/2015 23:16:23 ******/
ALTER TABLE [dbo].[Student]  WITH CHECK ADD FOREIGN KEY([deptID])
REFERENCES [dbo].[Department] ([depID])
GO
View Code

SQL查询语句:

with t1 as
(
    select b.stuID,SUM(a.score) as score from dbo.Score a 
    left join dbo.Student b on a.stuID = b.stuID
    group by b.stuID
),

t2 as
(
    select a.stuID,a.stuName,a.deptID,b.depName from dbo.Student a 
    left join Department b on a.deptID = b.depID
),

t3 as
(
    select rank() OVER(partition by deptID order by score desc) as RowId,
    t2.stuID,t2.stuName,t2.deptID,t2.depName,t1.score from t1 
    left join t2 on t1.stuID = t2.stuID
)

select stuID,stuName,deptID,depName,score from t3 where RowId = 1

查询结果:

与row_rumber不同,rank考虑到了over子句中排序字段值相同的情况。如果使用row_number替换rank,则deptID为2的记录只会有1条。

posted @ 2015-07-10 10:19  guwei4037  阅读(1629)  评论(0编辑  收藏  举报