22,210
社区成员
发帖
与我相关
我的任务
分享
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
*/
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
*/
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
*/
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
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
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
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
*/