欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

例说SQLServer2008聚合函数

程序员文章站 2024-01-29 21:35:58
...

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 CREATE TABLE tb_Class( classid int NOT NULL IDENTITY, classname NVARCHAR(16) NOT NULL, students int NOT NULL, teacher NVARCHAR(16) NOT NULL, PRIMARY KEY(classid)) INSERT INTO tb_Class(classname,students,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

CREATE TABLE tb_Class(

classid int NOT NULL IDENTITY,

classname NVARCHAR(16) NOT NULL,

students int NOT NULL,

teacher NVARCHAR(16) NOT NULL,

PRIMARY KEY(classid))

INSERT INTO tb_Class(classname,students,teacher)

VALUES

('1班',25,'yl'),('2班',18,'wsp'),('3班',24,'yl'),('4班',25,'zhy'),

('5班',22,'wsp'),('6班',25,'yl'),('7班',20,'zhy'), ('8班',22,'wsp')

SELECT SUM(students) FROM tb_Class GROUP BY teacher

SELECT COUNT(students) FROM tb_Class GROUP BY teacher

SELECT AVG(students) FROM tb_Class GROUP BY teacher

SELECT MAX(students) FROM tb_Class GROUP BY teacher

SELECT MIN(students) FROM tb_Class GROUP BY teacher