Transact-SQL UNION 集合运算符可在视图内使用,将单独表的两个或多个查询的结果组合到单一的结果集中。这在用户看来是一个单独的表,称为分区视图。例如,如果一个表包含华盛顿的销售数据,另一个表包含加利福尼亚的销售数据,则可以对这两个表使用 UNION 创建一个视图。该视图代表这两个地区的销售数据
你的那些规则,是哪国的规则? create table tb(id int,col int) insert into tb select 1,3476 insert into tb select 1,457 insert into tb select 1,8367 insert into tb select 2,736 insert into tb select 2,66 insert into tb select 3,37865 insert into tb select 3,237 insert into tb select 3,5731 go create view gettb as ( select * from tb where id=1 union all select top 3 * from tb where id>1 order by col desc union all select id,sum(col) from tb group by id having sum(col)>10000 union all select distinct id,col from tb where id=3 ) go select * from gettb go drop view gettb drop table tb /* id col ----------- ----------- 1 3476 1 457 1 8367 3 37865 3 5731 2 736 1 12300 3 43833 3 237 3 5731 3 37865(11 行受影响)*/
create table tb(id int,col int)
insert into tb select 1,3476
insert into tb select 1,457
insert into tb select 1,8367
insert into tb select 2,736
insert into tb select 2,66
insert into tb select 3,37865
insert into tb select 3,237
insert into tb select 3,5731
go
create view gettb as
(
select * from tb where id=1
union all
select top 3 * from tb where id>1 order by col desc
union all
select id,sum(col) from tb group by id having sum(col)>10000
union all
select distinct id,col from tb where id=3
)
go
select * from gettb
go
drop view gettb
drop table tb
/*
id col
----------- -----------
1 3476
1 457
1 8367
3 37865
3 5731
2 736
1 12300
3 43833
3 237
3 5731
3 37865(11 行受影响)*/