创建视图

doubagui 2011-10-17 05:49:38
有个表
有字段 F1,F2,F3,F4,F5 其中F1-F5字段的值范围在1-4之间,其中有两字段的值都为4,但是不知道是哪个字段,
现在要选出值不为4的另外三个字段(可理解为选出值小于4的三个字段)创建成一个视图
例如:(事先不知道那个字段的值全为4)
F1 F2 F3 F4 F5
4 3 2 4 2
4 1 3 4 2
4 1 3 4 2
...
创建的视图如下:
T1 T2 T3
3 2 2
1 3 2
1 3 2
...


...全文
304 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2011-10-18
  • 打赏
  • 举报
回复
动态行转列视图方法,想到用存储过程+OPENROWSET可解决楼主的问题

刚把楼主的问题整理了一篇博文;
http://blog.csdn.net/roy_88/article/details/6883078


longai123 2011-10-18
  • 打赏
  • 举报
回复
头晕了...
中国风 2011-10-17
  • 打赏
  • 举报
回复
9取7

use Tempdb
go
--> -->

if not object_id(N'T') is null
drop table T
Go
create table t(F1 int,F2 int,F3 int,F4 int,F5 int,F6 int,F7 int,F8 int,F9 int)
insert into t select 4,3,2,4,2,3,4,2,6
insert into t select 4,1,3,4,2,4,2,3,5
insert into t select 4,1,3,4,2,6,3,4,5
insert into t select 4,2,1,4,3,2,4,1,3
Go

if OBJECT_ID('v_T') is not null
drop view v_T
go
Create view v_T
as
with b
as
(
Select
*
from
(select *, ROW_NUMBER()over( order by (select 1)) as row from T) as a
UNPIVOT
(VALUE FOR Cols IN([F1],[F2],[F3],[F4],[F5],[F6],[F7],[F8],[F9]))AS b
),b2
as
(
select
VALUE,COls,Groups=ROW_NUMBER()over(partition by Cols order by row),DENSE_RANK()over(order by Cols) as row
from b
where
not exists(select 1 from b as c where c.Cols=b.Cols and VALUE=4 having COUNT(c.row)=(select MAX(row) from b))
)
select
max(case when row=1 then VALUE end) as T1,
max(case when row=2 then VALUE end) as T2,
max(case when row=3 then VALUE end) as T3,
max(case when row=4 then VALUE end) as T4,
max(case when row=5 then VALUE end) as T5,
max(case when row=6 then VALUE end) as T6,
max(case when row=7 then VALUE end) as T7
from b2
group by Groups

go

select * from v_T

/*
T1 T2 T3 T4 T5 T6 T7
3 2 2 3 4 2 6
1 3 2 4 2 3 5
1 3 2 6 3 4 5
2 1 3 2 4 1 3
*/
中国风 2011-10-17
  • 打赏
  • 举报
回复
改改
use Tempdb
go
--> -->

if not object_id(N'T') is null
drop table T
Go
Create table T([F1] int,[F2] int,[F3] int,[F4] int,[F5] int)
Insert T
select 4,3,2,4,2 union all
select 4,1,3,4,2 union all
select 4,1,3,4,2
Go
if OBJECT_ID('v_T') is not null
drop view v_T
go
Create view v_T
as
with b
as
(
Select
*
from
(select *, ROW_NUMBER()over( order by (select 1)) as row from T) as a
UNPIVOT
(VALUE FOR Cols IN([F1],[F2],[F3],[F4],[F5]))AS b
),b2
as
(
select
VALUE,COls,Groups=ROW_NUMBER()over(partition by Cols order by row),DENSE_RANK()over(order by Cols) as row
from b
where
not exists(select 1 from b as c where c.Cols=b.Cols and VALUE=4 having COUNT(c.row)=(select MAX(row) from b))
)
select
max(case when row=1 then VALUE end) as T1,
max(case when row=2 then VALUE end) as T2,
max(case when row=3 then VALUE end) as T3
from b2
group by Groups

go

select * from v_T

/*
T1 T2 T3
3 2 2
1 3 2
1 3 2
*/
中国风 2011-10-17
  • 打赏
  • 举报
回复
这样的视图有意义?

use Tempdb
go
--> -->

if not object_id(N'T') is null
drop table T
Go
Create table T([F1] int,[F2] int,[F3] int,[F4] int,[F5] int)
Insert T
select 4,3,2,4,2 union all
select 4,1,3,4,2 union all
select 4,1,3,4,2
Go
if OBJECT_ID('v_T') is not null
drop view v_T
go
Create view v_T
as
with b
as
(
Select
*,ROW_NUMBER()over( order by (select 1)) as row
from T
UNPIVOT
(VALUE FOR Cols IN([F1],[F2],[F3],[F4],[F5]))AS b
),b2
as
(
select
VALUE,COls,Groups=ROW_NUMBER()over(partition by Cols order by row),DENSE_RANK()over(order by Cols) as row
from b
where not exists(select 1 from b as c where c.COls=b.COls and VALUE=4)
)
select
max(case when row=1 then VALUE end) as T1,
max(case when row=2 then VALUE end) as T2,
max(case when row=3 then VALUE end) as T3
from b2
group by Groups

go

select * from v_T
/*
T1 T2 T3
3 2 2
1 3 2
1 3 2
*/
-晴天 2011-10-17
  • 打赏
  • 举报
回复
关键不在于除了4以外其他是几,关键是要找出全是4的列,并将它去除.
如果是用其他处理方法还好弄,你要视图,要一句话完成,还不能用变量,临时表等其他辅助手段,上面那个,我觉得已经够简单的了.
不过,还是进一步优化一下:
create table tb(F1 int,F2 int,F3 int,F4 int,F5 int,F6 int,F7 int,F8 int,F9 int)
insert into tb select 4,3,2,4,2,3,4,2,6
insert into tb select 4,1,3,4,2,4,2,3,5
insert into tb select 4,1,3,4,2,6,3,4,5
insert into tb select 4,2,1,4,3,2,4,1,3
go
create view v1 as
select rn,
max(case when id=1 then f1 end)f1,
max(case when id=2 then f1 end)f2,
max(case when id=3 then f1 end)f3,
max(case when id=4 then f1 end)f4,
max(case when id=5 then f1 end)f5,
max(case when id=6 then f1 end)f6,
max(case when id=7 then f1 end)f7
from(
select (rn-1)/(select COUNT(*) from tb)+1 id,(rn-1)%(select COUNT(*) from tb)+1 rn,f1 from(
select row_number()over(order by (select 1))rn,* from(
select f1 from tb where exists(select 1 from tb where f1<>4)
union all
select f2 from tb where exists(select 1 from tb where f2<>4)
union all
select f3 from tb where exists(select 1 from tb where f3<>4)
union all
select f4 from tb where exists(select 1 from tb where f4<>4)
union all
select f5 from tb where exists(select 1 from tb where f5<>4)
union all
select f6 from tb where exists(select 1 from tb where f6<>4)
union all
select f7 from tb where exists(select 1 from tb where f7<>4)
union all
select f8 from tb where exists(select 1 from tb where F8<>4)
union all
select f9 from tb where exists(select 1 from tb where F9<>4)
)t
)t1
)t2 group by rn
go
select * from v1
/*
rn f1 f2 f3 f4 f5 f6 f7
-------------------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 3 2 2 3 4 2 6
2 1 3 2 4 2 3 5
3 1 3 2 6 3 4 5
4 2 1 3 2 4 1 3
警告: 聚合或其他 SET 操作消除了 Null 值。

(4 行受影响)
*/

go
drop table tb
--drop view v1


方法,用列转行,将内容拆分,消除都为4的列,然后再根据总行数获得行序号,再作行转列操作.
doubagui 2011-10-17
  • 打赏
  • 举报
回复
意思就是,如果有9个字段,那么有两个字段的值都是4,其他字段的值是在1-3之间,那么创建的视图是去除了全是4的那两个字段!
doubagui 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 qianjin036a 的回复:]
9取7(中间的处理过程比上面的简单一些):

SQL code
create table tb(F1 int,F2 int,F3 int,F4 int,F5 int,F6 int,F7 int,F8 int,F9 int)
insert into tb select 4,3,2,4,2,3,4,2,6
insert into tb select 4,1,3,4,2,4,2,3,5
in……
[/Quote]
要求有4的字段,那么这个字段全是4(其他字段的值都小于4,1-3之间,不存在其他值),这样程序应该可以简单点!
-晴天 2011-10-17
  • 打赏
  • 举报
回复
9取7(中间的处理过程比上面的简单一些):
create table tb(F1 int,F2 int,F3 int,F4 int,F5 int,F6 int,F7 int,F8 int,F9 int)
insert into tb select 4,3,2,4,2,3,4,2,6
insert into tb select 4,1,3,4,2,4,2,3,5
insert into tb select 4,1,3,4,2,6,3,4,5
insert into tb select 4,2,1,4,3,2,4,1,3
go
create view v1 as
select rn,
max(case when id=1 then f1 end)f1,
max(case when id=2 then f1 end)f2,
max(case when id=3 then f1 end)f3,
max(case when id=4 then f1 end)f4,
max(case when id=5 then f1 end)f5,
max(case when id=6 then f1 end)f6,
max(case when id=7 then f1 end)f7
from(
select (rn1-1)/(select COUNT(*) from tb)+1 id,rn,f1 from(
select row_number()over(order by id,rn)rn1,* from(
select 1 as id,row_number()over(order by(select 1))rn,f1 from tb where exists(select 1 from tb where f1<>4)
union all
select 2,row_number()over(order by(select 1))rn,f2 from tb where exists(select 1 from tb where f2<>4)
union all
select 3,row_number()over(order by(select 1))rn,f3 from tb where exists(select 1 from tb where f3<>4)
union all
select 4,row_number()over(order by(select 1))rn,f4 from tb where exists(select 1 from tb where f4<>4)
union all
select 5,row_number()over(order by(select 1))rn,f5 from tb where exists(select 1 from tb where f5<>4)
union all
select 6,row_number()over(order by(select 1))rn,f6 from tb where exists(select 1 from tb where f6<>4)
union all
select 7,row_number()over(order by(select 1))rn,f7 from tb where exists(select 1 from tb where f7<>4)
union all
select 8,row_number()over(order by(select 1))rn,f8 from tb where exists(select 1 from tb where F8<>4)
union all
select 9,row_number()over(order by(select 1))rn,f9 from tb where exists(select 1 from tb where F9<>4)
)t
)t1
)t2 group by rn
go
select * from v1
/*
rn f1 f2 f3 f4 f5 f6 f7
-------------------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 3 2 2 3 4 2 6
2 1 3 2 4 2 3 5
3 1 3 2 6 3 4 5
4 2 1 3 2 4 1 3
警告: 聚合或其他 SET 操作消除了 Null 值。

(4 行受影响)
*/

go
drop table tb
drop view v1

doubagui 2011-10-17
  • 打赏
  • 举报
回复
因为我又可能是9个字段取7个字段
doubagui 2011-10-17
  • 打赏
  • 举报
回复
有没有简单点语句搞定
-晴天 2011-10-17
  • 打赏
  • 举报
回复
写得繁了点儿,但可以用了:
create table tb(F1 int,F2 int,F3 int,F4 int,F5 int)
insert into tb select 4,3,2,4,2
insert into tb select 4,1,3,4,2
insert into tb select 4,1,3,4,2
insert into tb select 4,2,1,4,3
go
create view v1 as
select rn,
max(case when id=1 then f1 end)f1,
max(case when id=2 then f1 end)f2,
max(case when id=3 then f1 end)f3
from(
select (case when rn=id2 then 3 when id1=1 then 1 else 2 end)id,rn,f1 from(
select row_number()over(order by id desc)id2,id1,rn1,id,rn,f1 from(
select (case when rn1=rn then 1 else id end)id1,* from(
select row_number()over(order by id,rn)rn1,* from(
select 1 as id,row_number()over(order by(select 1))rn,f1 from tb where exists(select 1 from tb where f1<>4)
union all
select 2,row_number()over(order by(select 1))rn,f2 from tb where exists(select 1 from tb where f2<>4)
union all
select 3,row_number()over(order by(select 1))rn,f3 from tb where exists(select 1 from tb where f3<>4)
union all
select 4,row_number()over(order by(select 1))rn,f4 from tb where exists(select 1 from tb where f4<>4)
union all
select 5,row_number()over(order by(select 1))rn,f5 from tb where exists(select 1 from tb where f5<>4)
)t
)t1
)t2
)t3
)t4 group by rn
go
select * from v1
/*
rn f1 f2 f3
-------------------- ----------- ----------- -----------
1 3 2 2
2 1 3 2
3 1 3 2
4 2 1 3
警告: 聚合或其他 SET 操作消除了空值。

(4 行受影响)

*/

go
drop table tb
drop view v1
中国风 2011-10-17
  • 打赏
  • 举报
回复
use Tempdb
go
--> -->

if not object_id(N'T') is null
drop table T
Go
Create table T([F1] int,[F2] int,[F3] int,[F4] int,[F5] int)
Insert T
select 4,3,2,4,2 union all
select 4,1,3,4,2 union all
select 4,1,3,4,2
Go
CREATE PROCEDURE p1
as
declare @s nvarchar(2000),@i int
set @i=1
;with b
as
(
Select
*
from T
UNPIVOT
(VALUE FOR COls IN([F1],[F2],[F3],[F4],[F5]))AS b
)
select @s=isnull(@s+',',' select ')+quotename(Cols)+' as T'+rtrim(@i),@i=@i+1 from b where not exists(select 1 from b as c where c.COls=b.COls and VALUE=4) group by Cols
exec(@s+' from T')
go

exec p1
/*
T1 T2 T3
3 2 2
1 3 2
1 3 2
*/
中国风 2011-10-17
  • 打赏
  • 举报
回复
這是存儲過程不是視圖啊
-晴天 2011-10-17
  • 打赏
  • 举报
回复
因为你的查询列不定,所以:
如果要创建视图,这就比较麻烦了.
如果是要查询,那要用if else 结构来跳转到不同的查询语句,也可以用动态查询.
第一篇 Oracle管理配置 第1章 Oracle安装配置(教学视频:10分钟) 23 1.1 Oracle简介 23 1.1.1 数据库术语 23 1.1.2 主流数据库简介 24 1.1.3 Oracle数据库的特点 24 1.2 安装Oracle数据库 25 1.2.1 Oracle数据库的版本变迁及安装环境 25 1.2.2 安装过程 26 1.2.3 安装中需要注意的问题 27 1.3 本章小结 28 1.4 习题 28 第2章 Oracle常用工具(教学视频:7分钟) 29 2.1 Net Configuration Assistant(网络配置助手) 29 2.1.1 监听程序配置 29 2.1.2 命名方法配置 31 2.1.3 本地Net服务名配置 32 2.2 Net Manager(网络管理员) 34 2.3 本章实例 36 2.4 本章小结 38 2.5 习题 38 第3章 SQL Plus和PL/SQL(教学视频:11分钟) 39 3.1 SQL Plus与PL/SQL简介 39 3.2 使用SQL Plus 40 3.2.1 登录SQL Plus 40 3.2.2 SQL Plus输出结果的格式化 41 3.2.3 SQL Plus小结 46 3.3 PL/SQL 46 3.3.1 PL/SQL常用开发工具 46 3.3.2 开发一个简单的PL/SQL程序 48 3.4 本章实例 49 3.5 本章小结 50 3.6 习题 50 第二篇 Oracle数据库对象 第4章 Oralce数据库(教学视频:15分钟) 51 4.1 创建Oracle数据库 51 4.2 Oracle数据库的相关术语 52 4.2.1 数据库 53 4.2.2 数据库实例和SID 53 4.2.3 ORACLE_SID 54 4.3 Oracle数据库的备份与恢复 55 4.3.1 逻辑备份/恢复(导出/导入) 55 4.3.2 物理备份/恢复 56 4.3.3 利用PL/SQL Developer备份数据库 60 4.4 本章实例 61 4.5 本章小结 61 4.6 习题 62 第5章 Oracle数据表对象(教学视频:42分钟) 63 5.1 Oracle表空间 63 5.1.1 Oracle表空间简介 63 5.1.2 创建Oracle表空间 64 5.1.3 查看表空间 66 5.1.4 修改数据库默认表空间 67 5.1.5 修改表空间名称 68 5.1.6 删除表空间 69 5.2 创建Oracle数据表 70 5.2.1 利用工具创建数据表 70 5.2.2 利用工具查看数据表 71 5.2.3 利用命令创建数据表 72 5.2.4 利用命令查看表结构 72 5.3 修改Oracle数据表结构 73 5.3.1 利用工具修改数据表结构 73 5.3.2 利用命令修改数据表结构 74 5.4 删除数据表 75 5.4.1 利用工具删除数据表 76 5.4.2 利用SQL语句删除数据表 76 5.5 备份/恢复数据表 76 5.5.1 利用工具备份/恢复数据表 77 5.5.2 利用命令备份/恢复数据表 82 5.6 临时表 83 5.6.1 临时表简介 83 5.6.2 会话级临时表 84 5.6.3 事务级临时表 85 5.6.4 查看临时表在数据库中的信息 86 5.6.5 临时表的应用场景 86 5.7 特殊的表dual 87 5.7.1 分析dual表 87 5.7.2 dual表的应用场景 87 5.7.3 修改dual表对查询结果的影响 88 5.8 本章实例 89 5.9 本章小结 90 5.10 习题 90 第6章 约束(教学视频:43分钟) 91 6.1 主键约束 91 6.1.1 主键简介 91 6.1.2 创建主键约束 92 6.1.3 修改表的主键约束 94 6.1.4 主键应用场景 96 6.2 外键约束 97 6.2.1 外键简介 97 6.2.2 创建外键约束 97 6.2.3 级联更新与级联删除 100 6.2.4 修改外键属性 102 6.2.5 外键使用 104 6.3 唯一性约束 105 6.3.1 唯一性约束简介 105 6.3.2 创建唯一性约束 105 6.3.3 修改唯一性约束 107 6.3.4 唯一性约束的使用 108 6.4 检查约束 108 6.4.1 检查约束简介 108 6.4.2 创建检查约束 108 6.4.3 修改检查约束 110 6.4.4 检查约束的使用 111 6.5 默认值约束 111 6.5.1 默认值约束简介 112 6.5.2 创建默认值约束 112 6.5.3 修改默认值约束 113 6.6 本章实例 115 6.7 本章小结 116 6.8 习题 116 第7章 视图(教学视频:50分钟) 117 7.1 关系视图 117 7.1.1 建立关系视图 117 7.1.2 修改/删除视图 118 7.1.3 联接视图 120 7.1.4 编译视图 122 7.1.5 使用force选项强制创建视图 124 7.1.6 利用视图更新数据表 125 7.1.7 with check option选项 126 7.1.8 关系视图小结 128 7.2 内嵌视图 128 7.2.1 内嵌视图简介 128 7.2.2 内嵌视图的使用 128 7.2.3 内嵌视图小结 130 7.3 对象视图 131 7.3.1 对象视图简介 131 7.3.2 对象视图简介 131 7.4 物化视图 133 7.4.1 物化视图简介 133 7.4.2 物化视图的使用 133 7.4.3 物化视图的数据加载 135 7.4.4 物化视图的数据更新 135 7.4.5 查询重写 136 7.5 本章小结 136 7.6 本章实例 137 7.7 习题 137 第8章 函数与存储过程(教学视频:48分钟) 138 8.1 函数 138 8.1.1 函数简介 138 8.1.2 创建函数 139 8.1.3 函数中的括号 140 8.1.4 函数的参数 141 8.1.5 函数的确定性 142 8.1.6 典型函数举例 143 8.2 存储过程 144 8.2.1 存储过程简介 144 8.2.2 创建存储过程 144 8.2.3 存储过程的参数——IN参数 146 8.2.4 存储过程的参数——OUT参数 147 8.2.5 存储过程的参数——IN OUT参数 149 8.2.6 存储过程的参数——参数顺序 149 8.2.7 存储过程的参数——参数的默认值 152 8.2.8 存储过程的参数——参数顺序总结 153 8.3 程序包 153 8.3.1 规范 153 8.3.2 主体 155 8.3.3 调用程序包中的函数/存储过程 157 8.3.4 程序包中的变量 158 8.4 本章实例 159 8.5 本章小结 161 8.6 习题 161 …… 第9章 游标(教学视频:36分钟) 162 第10章 触发器(教学视频:58分钟) 178 第11章 序列(教学视频:28分钟) 206 第12章 用户角色与权限控制(教学视频:45分钟) 215 第三篇 Oracle中的SQL 第13章 Oracle数据类型(教学视频:21分钟) 231 第14章 Oracle中的函数与表达式(教学视频:111分钟) 240 第15章 Oracle中的控制语句(教学视频:16分钟) 282 第16章 SQL查询(教学视频:55分钟) 290 第17章 SQL更新数据(教学视频:34分钟) 319 第四篇 Oracle编程高级应用 第18章 数据库速度优化与数据完整性(教学视频:32分钟) 332 第19章 数据一致性与事务管理(教学视频:46分钟) 341 第20章 并发控制(教学视频:35分钟) 356 第21章 Oracle中的正则表达式(教学视频:29分钟) 369 第五篇 Oracle与编程语言综合使用实例 第22章 Oracle在Java开发中的应用(教学视频:38分钟) 376 第23章 Oracle在C#开发中的应用(教学视频:12分钟) 391

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧