码迷,mamicode.com
首页 > 数据库 > 详细

Sql Group by 使用

时间:2015-11-22 13:56:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

CREATE TABLE StuCourseScore
(
ID int, 
Name nvarchar(10),
Course nvarchar(10),
Score int
)
INSERT StuCourseScore VALUES(1,‘张三‘,‘语文‘,80)
INSERT StuCourseScore VALUES(1,‘张三‘,‘数学‘,89)
INSERT StuCourseScore VALUES(1,‘张三‘,‘英语‘,90)

INSERT StuCourseScore VALUES(2,‘李四‘,‘语文‘,100)
INSERT StuCourseScore VALUES(2,‘李四‘,‘数学‘,79)
INSERT StuCourseScore VALUES(2,‘李四‘,‘英语‘,95)

SELECT * FROM StuCourseScore

SELECT --group的字段或者聚合函数
	ID, 
	sum(
	case
		when Course=‘语文‘ then Score
	end) as "语文成绩",
	sum(
	case
		when Course=‘数学‘ then Score
	end) as "数学成绩",
	sum(
	case
		when Course=‘英语‘ then Score
	end) as "英语成绩"
FROM StuCourseScore
GROUP BY ID

--以后名字加上
SELECT Name FROM StuCourseScore
group BY Name

  

Sql Group by 使用

标签:

原文地址:http://www.cnblogs.com/leee/p/4985710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!