代码大概80行左右
本系列,几乎都是代码,记得当时写的时候用的是微软的官方实例数据库AdventureWorks_Data.mdf、AdventureWorks_Log.ldf来运行的。
下载链接:链接: https://pan.baidu.com/s/1pMdLz6N 密码: xvhu
或者回复“AdventureWorks”来获取链接。
不知道随便想的里面有没有跟读者同名的
--create table EmpTabledrop table EmpTableCREATE TABLE EmpTable( EmpName nchar(), Region nvarchar(), Department nvarchar(), Salary money)--insert records into the tableinsert into EmpTable values(,,,),(,,,),(,,,),(,,,),(,,,),(,,,)select * from EmpTable-- ).查看所有员工平均工资select avg(Salary) as from EmpTable-- ).按照地区分组,查看每个地区的平均工资select Region,avg(Salary) from EmpTable group by Regionselect Region,sum(Salary) from EmpTable group by Regionselect Region,avg(Salary) from EmpTable group by Region having avg(Salary)>-- ).按照部门分组,查看每个部门的平均工资select Department,avg(Salary) from EmpTable group by Department--).同时按照地区和部门分组,显示如山东地区人力资源部的平均工资的信息select Region,Department,avg(Salary) from EmpTable group by Region,Department--).同时按照地区和部门、地区、部门分组select Region,Department,avg(Salary) from EmpTablegroup bygrouping sets( (Region,Department), (Region), (Department))--).按照部门分组的同时显示总的平均select Region,sum(Salary) from EmpTable group by Regionselect sum(Salary) from EmpTableselect Region,sum(Salary) from EmpTable group by rollup(Region)--Region--不分组select Region,Department,sum(Salary) from EmpTable group by rollup(Region,Department)--Region,Department--Region--不分组-- rollup(A,B,C)-- A,,-- A,-- A -- 不分组--).同时按照地区和部门分组时,同时显示地区平均,部门平均和总平均select Region,Department,sum(Salary) from EmpTable group by cube(Region,Department)--Region+Department--Region--Department--不分组--cube(A,B,C)--A------A,--A,--,--A,,--不分组--周末了,加班的继续加班,休班的好好休息吧!