看 SQL SERVER 2005范例代码查询辞典(练习 总结)

1:使用 povit 把列值转换为多列值和聚会数据 

if object_id('tb1') is not null drop table tb1
CREATE TABLE [dbo].[tb1](
[name] [nvarchar](50) NULL,
[coure] [nvarchar](50) NULL,
[score] [int] NULL
) ON [PRIMARY]

insert into tb1 values(N'张三',N'语文',77)

insert into tb1 values(N'张三',N'数学',45) 

insert into tb1 values(N'张三',N'英语',86) 

insert into tb1 values(N'李四',N'语文',56)

insert into tb1 values(N'李四',N'数学',35) 

insert into tb1 values(N'李四',N'英语',87)

select * from tb1 -----tb1 要旋转的数据表
pivot(
max(score) for             ----在某列上使用的聚合函数
coure                             ----用户创建列头的列
in([语文],[数学],[英语]) -----要从旋转列中旋转的值
)as b                             -----旋转结果集的表别名

name                                               语文          数学          英语
-------------------------------------------------- ----------- ----------- -----------
张三                                                 77          45          86
李四                                                 56          35          87

2:使用unpivot  行转列

if object_id('tb') is not null drop table tb
CREATE TABLE [dbo].[tb](
id int not NULL,
number1 bigint ,
number2 bigint ,
number3 bigint 
) ON [PRIMARY]

insert into tb values(1,123456,234567,345678)
insert into tb values(2,456789,435235,645643)
insert into tb values(3,345435,null,null)

select id,numbertype,value
from tb
unpivot(
value for numbertype in([number1],[number2],[number3])
)as b

id          numbertype                                                                                                                       value
----------- -------------------------------------------------------------------------------------------------------------------------------- --------------------
1           number1                                                                                                                          123456
1           number2                                                                                                                          234567
1           number3                                                                                                                          345678
2           number1                                                                                                                          456789
2           number2                                                                                                                          435235
2           number3                                                                                                                          645643
3           number1                                                                                                                          345435

3:except 和intersect 的使用

select * into #temp2 from tb where id<=4
select * into #temp3 from tb where id>=3
select * from #temp2
select * from #temp3 
id          name                                               coure                                              score
----------- -------------------------------------------------- -------------------------------------------------- -----------
1           张三                                                 语文                                                 77
2           张三                                                 数学                                                 45
3           张三                                                 英语                                                 86
4           李四                                                 语文                                                 56

id          name                                               coure                                              score
----------- -------------------------------------------------- -------------------------------------------------- -----------
3           张三                                                 英语                                                 86
4           李四                                                 语文                                                 56
5           李四                                                 数学                                                 35
6           李四                                                 英语                                                 87

select * from #temp2 --------查询#temp2 表存在,#temp3表中不存在的数据
except
select * from #temp3

id          name                                               coure                                              score
----------- -------------------------------------------------- -------------------------------------------------- -----------
1           张三                                                 语文                                                 77
2           张三                                                 数学                                                 45
select * from #temp2 --------查询#temp2 表和#temp3表中都存在的数据
intersect
select * from #temp3

id          name                                               coure                                              score
----------- -------------------------------------------------- -------------------------------------------------- -----------
3           张三                                                 英语                                                 86
4           李四                                                 语文                                                 56

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值