SQL 基础面试题01☆☆☆_分组统计等怎么加虚拟列1班和2班相差的分数怎么用sql计算

Posted 平凡加班狗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 基础面试题01☆☆☆_分组统计等怎么加虚拟列1班和2班相差的分数怎么用sql计算相关的知识,希望对你有一定的参考价值。

SQL 基础面试题01☆☆☆系列文章目录

表结构

CREATE TABLE [dbo].[AScore](
	[Name] [nvarchar](50) NULL,
	[SubjectName] [nvarchar](50) NULL,
	[Score] [decimal](18, 2) NULL,
	[ClassNo] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[AScoreZone](
	[ClassNo] [nvarchar](50) NULL,
	[ClassName] [nvarchar](50) NULL,
	[CityZone] [nvarchar](50) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[AScoreZone] ([ClassNo], [ClassName], [CityZone]) VALUES (N'1', N'一班', N'福田区')
INSERT [dbo].[AScoreZone] ([ClassNo], [ClassName], [CityZone]) VALUES (N'2', N'二班', N'福田区')
/****** Object:  Table [dbo].[AScore]    Script Date: 09/01/2017 21:22:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘峰', N'语文', CAST(90.00 AS Decimal(18, 2)), N'1')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘峰', N'数学', CAST(80.00 AS Decimal(18, 2)), N'1')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘峰', N'英语', CAST(80.11 AS Decimal(18, 2)), N'1')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘丹', N'语文', CAST(80.00 AS Decimal(18, 2)), N'2')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘丹', N'数学', CAST(98.00 AS Decimal(18, 2)), N'2')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘晶', N'英语', CAST(90.00 AS Decimal(18, 2)), N'2')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'刘晶', N'语文', CAST(90.00 AS Decimal(18, 2)), N'2')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'小浩', N'语文', CAST(100.00 AS Decimal(18, 2)), N'3')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'小浩', N'英语', CAST(89.00 AS Decimal(18, 2)), N'3')
INSERT [dbo].[AScore] ([Name], [SubjectName], [Score], [ClassNo]) VALUES (N'小浩', N'数学', CAST(11.00 AS Decimal(18, 2)), N'3')


1、将这3科弄成如下模式:

语文数学英语
刘晶XXX
刘丹XXX
刘峰XXX

答:

select Name,
	   SUM(case subjectName when '语文' then Score else 0 end) 语文,
	   SUM(case subjectName when '数学' then Score else 0 end) 数学, 
	   SUM(case subjectName when '英语' then Score else 0 end) 英语 
	   from AScore group by Name 

2、2班总分比1班总分高出多少的报表

高出分数
福田1班2班50分


分析

分析,怎么加虚拟列


分析:1班和2班相差的分数怎么用sql计算


所以我们也可以这样写

分析完 下面就可以正式开始写完整的sql了
最终答案

select CityZone 区,'1班','2班',(select 
((select sum(Score)二班总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo and ClassName='二班'group by ClassName)-
(select sum(Score)一班总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo and ClassName='一班'group by ClassName))
)'高出分数' from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo group by CityZone

3、附:完整sql代码

select * from AScore 
--第1题
select Name,
	   SUM(case subjectName when '语文' then Score else 0 end) 语文,
	   SUM(case subjectName when '数学' then Score else 0 end) 数学, 
	   SUM(case subjectName when '英语' then Score else 0 end) 英语 
	   from AScore group by Name 
	   
--第2题
select * from AScore
select * from AScoreZone

select * from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo


select CityZone 区,'1班','2班'from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo group by CityZone


-- 1班和2班相差的分数
select ClassName,sum(Score)总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo group by ClassName

select 
((select sum(Score)二班总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo and ClassName='二班'group by ClassName)-
(select sum(Score)一班总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo and ClassName='一班'group by ClassName))'高出分数'


select CityZone 区,'1班','2班',(select 
((select sum(Score)二班总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo and ClassName='二班'group by ClassName)-
(select sum(Score)一班总分 from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo and ClassName='一班'group by ClassName))
)'高出分数' from AScore,AScoreZone where AScore.ClassNo=AScoreZone.ClassNo group by CityZone


以上是关于SQL 基础面试题01☆☆☆_分组统计等怎么加虚拟列1班和2班相差的分数怎么用sql计算的主要内容,如果未能解决你的问题,请参考以下文章

MySQL分组汇总实例(面试题入门到拓展)

找工作再也不愁之面试题全覆盖-数据库篇

Java高频面试题基础篇01

sql经典50题

ORACLE分组查询和统计等

面试题精选 —— 数据库统计