SQL Server实用经典例句之二

1.在sqlserver创建用户临时表和系统临时表的方法,及两者的区别?
用户临时表:create table #xx(ID int, IDValues int)
系统临时表:create table ##xx(ID int, IDValues int)

区别:
用户临时表只对创建这个表的用户的Session可见,对其他进程是不可见的.
当创建它的进程消失时这个临时表就自动删除.

系统临时表对整个SQL Server实例都可见,但是所有访问它的Session都消失的时候,它也自动删除.

 2.已知原表(t_salary)
year salary
2000 1000
2001 1500
2002 2000
2003 2200
先要实现显示结果(salary为以前的工资和)
year salary
2000 1000
2001 2500

--解法:
select year, (select sum(salary) from t_salary b where b.year <= a.year) salary from t_salary a group by year

3.year month total
1996 1 3000
1996 3 4000
1996 7 5000
1997 3 4000
1997 6 3000
.
要求按如下格式输出:
year m1,m2,m3,m4
year 为年,m1等为季度,要求按上行输出

create table Outputs (year char(4),month int,total int)

代码
--解法一:
select year,
sum(case when month<=3 then total else 0 endas M1,
sum(case when month>3 and month<=6 then total else 0 endas M2,
sum(case when month>6 and month<=9 then total else 0 endas M3,
sum(case when month>9 and month<=12 then total else 0 endas M2
from Outputs group by year

--解法二:
select year,
sum(case when month in(1,2,3then total else 0 endas M1,
sum(case when month in(4,5,6then total else 0 endas M2,
sum(case when month in(7,8,9then total else 0 endas M3,
sum(case when month in(10,11,12then total else 0 endas M2
from Outputs group by year

3.普通行列转换
问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果):
姓名 语文 数学 物理
---- ---- ---- ----
李四 74 84 94
张三 74 83 93
-------------------

create table tb(
姓名 varchar(10) ,
课程 varchar(10) ,
分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
insert into tb values('李四' , '历史' , 94)

代码
-----解法一:SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)---------
select 姓名,
max(case when 课程='语文' then 分数 endas 语文,
max(case when 课程='数学' then 分数 endas 数学,
max(case when 课程='物理' then 分数 endas 物理
from tb
group by 姓名


-----解法二:--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同) ---------
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql)


-----解法三:SQL SERVER 2005 静态SQL。
select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b

-----解法四:SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , ''+ 课程 from tb group by 课程
exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')

4.如果上述两表互相换一下:即表结构和数据为:
姓名 语文 数学 物理
张三 74 83 93
李四 74 84 94
想变成(得到如下结果):
姓名 课程 分数
---- ---- ----
李四 语文 74
李四 数学 84
李四 物理 94
张三 语文 74
张三 数学 83
张三 物理 93
--------------

create table tb(姓名 varchar(10) , 语文 int , 数学 int , 物理 int)
insert into tb values('张三',74,83,93)
insert into tb values('李四',74,84,94)
go

代码
--SQL SERVER 2000 静态SQL。
select * from
(
select 姓名 , 课程 = '语文' , 分数 = 语文 from tb
union all
select 姓名 , 课程 = '数学' , 分数 = 数学 from tb
union all
select 姓名 , 课程 = '物理' , 分数 = 物理 from tb
) t
order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 end

--SQL SERVER 2000 动态SQL。
--
调用系统表动态生态。
declare @sql varchar(8000)
select @sql = isnull(@sql + ' union all ' , '' ) + ' select 姓名 , [课程] = ' + quotename(Name , ''''+ ' , [分数] = ' + quotename(Name) + ' from tb'
from syscolumns
where name! = N'姓名' and ID = object_id('tb'--表名tb,不包含列名为姓名的其它列
order by colid asc
exec(@sql + ' order by 姓名 ')

--SQL SERVER 2005 动态SQL。
select 姓名 , 课程 , 分数 from tb unpivot (分数 for 课程 in([语文] , [数学] , [物理])) t

--SQL SERVER 2005 动态SQL,同SQL SERVER 2000 动态SQL。

原文地址:https://www.cnblogs.com/fredx/p/1697245.html