100分求一存储过程!!紧急!!!

时间:2021-04-20 11:53:55
现有3张数据表,table1(business int,funcno int,appno int)
               table2(userid int,business int,func1 int,func2 int,func3 int)
               table3(userid int,business int,app1 int,app2 int,app3 int,app4 int,app5 int,app6 int,app7 int,app8 int,app9 int)
table1的典型数据如下
business  funcno    appno
  1         1         3
  1         2         1 
  1         3         2
  2         1         4
  2         2         5
  2         3         6                
table2的典型数据如下
userid     business   func1   func2   func3    
  1           1         1       0       1     
  2           1         0       0       1      
  1           2         1       1       1     
  2           2         1       0       0
table3的典型数据如下
userid  business  app1  app2  app3  app4  app5  app6  app7 app8 app9 
  1        1       0      1    1      0     0     0    0     0    0
  2        1       0      1    0      0     0     0    0     0    0
  1        2       0      0    0      1     1     1    0     0    0
  2        2       0      0    0      1     0     0    0     0    0
可以看到table3的数据实际是由table1和table2的数据生成的,table1是table2和table2之间的转换表,即由一个business和funcno可以确定一个appno,如table2表中的第一行数据,func1与business一起对应app3,func2与business一起对应app1,func3与business一起对应app2,因此在tabel3中userid为1,business为1的那一行对应的app1为0,app2为1,app3为1,由于business没有对应app4~app9,因此app4~app9全为0
现在table1和table2数据均存在,需要生成table3的数据,怎样使用存储过程高效实现?

8 个解决方案

#1


http://expert.csdn.net/Expert/topic/2440/2440306.xml?temp=.6941645
 [交流]行列转换 


try:
declare @sql varchar(8000)
set @sql = 'select t1.userid,t1.business'
select @sql = @sql + ',sum(case funcno when '''+cast(funcno as varchar)+''' then cj else 0 end) [app'+cast(funcno as varchar)+']'
 from (select distinct funcno from table1) as a
select @sql = @sql+' from table2 t1 join table1 t2 on t1.business = t2.business group by t1.userid,t1.business '
exec(@sql)

#2


func1与business一起对应app3,那为什么APP3 的值是1,取AND运算吗?

app4  app5  app6  怎么对应???



#3


测试:
create table table1(business int,funcno int,appno int)
create table table2(userid int,business int,func1 int,func2 int,func3 int)
insert table1 select   1,         1 ,        3
union all select   1,         2    ,     1 
union all select   1 ,        3   ,      2
union all select   2  ,       1  ,       4
union all select   2   ,      2 ,        5
union all select   2    ,     3,         6                
insert table2 select   1,           1,         1,       0 ,      1     
union all select   2,           1  ,       0,       0    ,   1      
union all select   1 ,          2 ,        1 ,      1   ,    1     
union all select   2  ,         2,         1  ,     0  ,     0


declare @sql varchar(8000)
set @sql = 'select t1.userid,t1.business'
select @sql = @sql + ',IsNull(max(case when funcno = ''1'' and appno = '''+cast(appno as varchar)+''' then func1 
when funcno = ''2'' and appno = '''+cast(appno as varchar)+''' then func2
when funcno = ''3'' and appno = '''+cast(appno as varchar)+''' then func3
end),0) [app'+cast(appno as varchar)+']'
 from (select distinct funcno,appno from table1) as a
select @sql = @sql+' from table2 t1 join table1 t2 on t1.business = t2.business group by t1.userid,t1.business '
exec(@sql)

userid      business    app3        app4        app1        app5        app2        app6        
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
1           1           1           0           0           0           1           0
2           1           0           0           0           0           1           0
1           2           0           1           0           1           0           1
2           2           0           1           0           0           0           0


#4


declare @sql varchar(8000)
set @sql = 'select t1.userid,t1.business'
select @sql = @sql + ',IsNull(max(case when funcno = ''1'' and appno = '''+cast(appno as varchar)+''' then func1 
when funcno = ''2'' and appno = '''+cast(appno as varchar)+''' then func2
when funcno = ''3'' and appno = '''+cast(appno as varchar)+''' then func3
end),0) [app'+cast(appno as varchar)+']'
 from (select distinct funcno,appno from table1) as a
select @sql = @sql+',0 as app7,0 as app8,0 as app9 from table2 t1 join table1 t2 on t1.business = t2.business group by t1.userid,t1.business '
exec(@sql)


userid      business    app3        app4        app1        app5        app2        app6        app7        app8        app9        
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
1           1           1           0           0           0           1           0           0           0           0
2           1           0           0           0           0           1           0           0           0           0
1           2           0           1           0           1           0           1           0           0           0
2           2           0           1           0           0           0           0           0           0           0

#5


因为func1的值为1,所以对应的app3为1

#6


不用动态SQL语句的话
select a.userid,a.business,
IsNull(max(case when b.funcno = 1 and b.appno = 1 then func1 when b.funcno = 2 and b.appno = 1 then func2 when b.funcno = 3 and b.appno = 1 then func3  end ),0) as app1,
IsNull(max(case when b.funcno = 1 and b.appno = 2 then func1 when b.funcno = 2 and b.appno = 2 then func2 when b.funcno = 3 and b.appno = 2 then func3  end ),0) as app2,
IsNull(max(case when b.funcno = 1 and b.appno = 3 then func1 when b.funcno = 2 and b.appno = 3 then func2 when b.funcno = 3 and b.appno = 3 then func3  end ),0) as app3,
IsNull(max(case when b.funcno = 1 and b.appno = 4 then func1 when b.funcno = 2 and b.appno = 4 then func2 when b.funcno = 3 and b.appno = 4 then func3  end ),0) as app4,
IsNull(max(case when b.funcno = 1 and b.appno = 5 then func1 when b.funcno = 2 and b.appno = 5 then func2 when b.funcno = 3 and b.appno = 5 then func3  end ),0) as app5,
IsNull(max(case when b.funcno = 1 and b.appno = 6 then func1 when b.funcno = 2 and b.appno = 6 then func2 when b.funcno = 3 and b.appno = 6 then func3  end ),0) as app6,
IsNull(max(case when b.funcno = 1 and b.appno = 7 then func1 when b.funcno = 2 and b.appno = 7 then func2 when b.funcno = 3 and b.appno = 7 then func3  end ),0) as app7,
IsNull(max(case when b.funcno = 1 and b.appno = 8 then func1 when b.funcno = 2 and b.appno = 8 then func2 when b.funcno = 3 and b.appno = 8 then func3  end ),0) as app8,
IsNull(max(case when b.funcno = 1 and b.appno = 9 then func1 when b.funcno = 2 and b.appno = 9 then func2 when b.funcno = 3 and b.appno = 9 then func3  end ),0) as app9
from table2 a 
join table1 b on a.business = a.business
group by a.userid,a.business

#7


--用查询语句,这样就行啦:
--查询处理
select userid,business
,app1=case when appno1=1 or appno2=1 or appno3=1 then 1 else 0 end
,app2=case when appno1=2 or appno2=2 or appno3=2 then 1 else 0 end
,app3=case when appno1=3 or appno2=3 or appno3=3 then 1 else 0 end
,app4=case when appno1=4 or appno2=4 or appno3=4 then 1 else 0 end
,app5=case when appno1=5 or appno2=5 or appno3=5 then 1 else 0 end
,app6=case when appno1=6 or appno2=6 or appno3=6 then 1 else 0 end
,app7=case when appno1=7 or appno2=7 or appno3=7 then 1 else 0 end
,app8=case when appno1=8 or appno2=8 or appno3=8 then 1 else 0 end
,app9=case when appno1=9 or appno2=9 or appno3=9 then 1 else 0 end
from(
select a.userid,a.business
,appno1=b.appno*a.func1
,appno2=c.appno*a.func2
,appno3=d.appno*a.func3
from table2 a
join(select business,appno from table1 
where funcno=1) b on a.business=b.business
join(select business,appno from table1 
where funcno=2) c on a.business=c.business
join(select business,appno from table1 
where funcno=3) d on a.business=d.business
) a

#8


--下面是数据测试

--测试数据
create table table1(business int,funcno int,appno int)
insert table1
select 1,1,3
union all select 1,2,1 
union all select 1,3,2
union all select 2,1,4
union all select 2,2,5
union all select 2,3,6

create table table2(userid int,business int,func1 int,func2 int,func3 int)

insert table2
select 1,1,1,0,1 
union all select 2,1,0,0,1
union all select 1,2,1,1,1 
union all select 2,2,1,0,0
go

--查询处理
select userid,business
,app1=case when appno1=1 or appno2=1 or appno3=1 then 1 else 0 end
,app2=case when appno1=2 or appno2=2 or appno3=2 then 1 else 0 end
,app3=case when appno1=3 or appno2=3 or appno3=3 then 1 else 0 end
,app4=case when appno1=4 or appno2=4 or appno3=4 then 1 else 0 end
,app5=case when appno1=5 or appno2=5 or appno3=5 then 1 else 0 end
,app6=case when appno1=6 or appno2=6 or appno3=6 then 1 else 0 end
,app7=case when appno1=7 or appno2=7 or appno3=7 then 1 else 0 end
,app8=case when appno1=8 or appno2=8 or appno3=8 then 1 else 0 end
,app9=case when appno1=9 or appno2=9 or appno3=9 then 1 else 0 end
from(
select a.userid,a.business
,appno1=b.appno*a.func1
,appno2=c.appno*a.func2
,appno3=d.appno*a.func3
from table2 a
join(select business,appno from table1 
where funcno=1) b on a.business=b.business
join(select business,appno from table1 
where funcno=2) c on a.business=c.business
join(select business,appno from table1 
where funcno=3) d on a.business=d.business
) a

go
--删除测试环境
drop table table1,table2

/*--测试结果
userid  business  app1  app2  app3  app4  app5  app6  app7  app8  app9
------- --------- ----- ----- ----- ----- ----- ----- ----- ----- ------
1       1         0     1     1     0     0     0     0     0     0
2       1         0     1     0     0     0     0     0     0     0
1       2         0     0     0     1     1     1     0     0     0
2       2         0     0     0     1     0     0     0     0     0

(所影响的行数为 4 行)

--*/

#1


http://expert.csdn.net/Expert/topic/2440/2440306.xml?temp=.6941645
 [交流]行列转换 


try:
declare @sql varchar(8000)
set @sql = 'select t1.userid,t1.business'
select @sql = @sql + ',sum(case funcno when '''+cast(funcno as varchar)+''' then cj else 0 end) [app'+cast(funcno as varchar)+']'
 from (select distinct funcno from table1) as a
select @sql = @sql+' from table2 t1 join table1 t2 on t1.business = t2.business group by t1.userid,t1.business '
exec(@sql)

#2


func1与business一起对应app3,那为什么APP3 的值是1,取AND运算吗?

app4  app5  app6  怎么对应???



#3


测试:
create table table1(business int,funcno int,appno int)
create table table2(userid int,business int,func1 int,func2 int,func3 int)
insert table1 select   1,         1 ,        3
union all select   1,         2    ,     1 
union all select   1 ,        3   ,      2
union all select   2  ,       1  ,       4
union all select   2   ,      2 ,        5
union all select   2    ,     3,         6                
insert table2 select   1,           1,         1,       0 ,      1     
union all select   2,           1  ,       0,       0    ,   1      
union all select   1 ,          2 ,        1 ,      1   ,    1     
union all select   2  ,         2,         1  ,     0  ,     0


declare @sql varchar(8000)
set @sql = 'select t1.userid,t1.business'
select @sql = @sql + ',IsNull(max(case when funcno = ''1'' and appno = '''+cast(appno as varchar)+''' then func1 
when funcno = ''2'' and appno = '''+cast(appno as varchar)+''' then func2
when funcno = ''3'' and appno = '''+cast(appno as varchar)+''' then func3
end),0) [app'+cast(appno as varchar)+']'
 from (select distinct funcno,appno from table1) as a
select @sql = @sql+' from table2 t1 join table1 t2 on t1.business = t2.business group by t1.userid,t1.business '
exec(@sql)

userid      business    app3        app4        app1        app5        app2        app6        
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
1           1           1           0           0           0           1           0
2           1           0           0           0           0           1           0
1           2           0           1           0           1           0           1
2           2           0           1           0           0           0           0


#4


declare @sql varchar(8000)
set @sql = 'select t1.userid,t1.business'
select @sql = @sql + ',IsNull(max(case when funcno = ''1'' and appno = '''+cast(appno as varchar)+''' then func1 
when funcno = ''2'' and appno = '''+cast(appno as varchar)+''' then func2
when funcno = ''3'' and appno = '''+cast(appno as varchar)+''' then func3
end),0) [app'+cast(appno as varchar)+']'
 from (select distinct funcno,appno from table1) as a
select @sql = @sql+',0 as app7,0 as app8,0 as app9 from table2 t1 join table1 t2 on t1.business = t2.business group by t1.userid,t1.business '
exec(@sql)


userid      business    app3        app4        app1        app5        app2        app6        app7        app8        app9        
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
1           1           1           0           0           0           1           0           0           0           0
2           1           0           0           0           0           1           0           0           0           0
1           2           0           1           0           1           0           1           0           0           0
2           2           0           1           0           0           0           0           0           0           0

#5


因为func1的值为1,所以对应的app3为1

#6


不用动态SQL语句的话
select a.userid,a.business,
IsNull(max(case when b.funcno = 1 and b.appno = 1 then func1 when b.funcno = 2 and b.appno = 1 then func2 when b.funcno = 3 and b.appno = 1 then func3  end ),0) as app1,
IsNull(max(case when b.funcno = 1 and b.appno = 2 then func1 when b.funcno = 2 and b.appno = 2 then func2 when b.funcno = 3 and b.appno = 2 then func3  end ),0) as app2,
IsNull(max(case when b.funcno = 1 and b.appno = 3 then func1 when b.funcno = 2 and b.appno = 3 then func2 when b.funcno = 3 and b.appno = 3 then func3  end ),0) as app3,
IsNull(max(case when b.funcno = 1 and b.appno = 4 then func1 when b.funcno = 2 and b.appno = 4 then func2 when b.funcno = 3 and b.appno = 4 then func3  end ),0) as app4,
IsNull(max(case when b.funcno = 1 and b.appno = 5 then func1 when b.funcno = 2 and b.appno = 5 then func2 when b.funcno = 3 and b.appno = 5 then func3  end ),0) as app5,
IsNull(max(case when b.funcno = 1 and b.appno = 6 then func1 when b.funcno = 2 and b.appno = 6 then func2 when b.funcno = 3 and b.appno = 6 then func3  end ),0) as app6,
IsNull(max(case when b.funcno = 1 and b.appno = 7 then func1 when b.funcno = 2 and b.appno = 7 then func2 when b.funcno = 3 and b.appno = 7 then func3  end ),0) as app7,
IsNull(max(case when b.funcno = 1 and b.appno = 8 then func1 when b.funcno = 2 and b.appno = 8 then func2 when b.funcno = 3 and b.appno = 8 then func3  end ),0) as app8,
IsNull(max(case when b.funcno = 1 and b.appno = 9 then func1 when b.funcno = 2 and b.appno = 9 then func2 when b.funcno = 3 and b.appno = 9 then func3  end ),0) as app9
from table2 a 
join table1 b on a.business = a.business
group by a.userid,a.business

#7


--用查询语句,这样就行啦:
--查询处理
select userid,business
,app1=case when appno1=1 or appno2=1 or appno3=1 then 1 else 0 end
,app2=case when appno1=2 or appno2=2 or appno3=2 then 1 else 0 end
,app3=case when appno1=3 or appno2=3 or appno3=3 then 1 else 0 end
,app4=case when appno1=4 or appno2=4 or appno3=4 then 1 else 0 end
,app5=case when appno1=5 or appno2=5 or appno3=5 then 1 else 0 end
,app6=case when appno1=6 or appno2=6 or appno3=6 then 1 else 0 end
,app7=case when appno1=7 or appno2=7 or appno3=7 then 1 else 0 end
,app8=case when appno1=8 or appno2=8 or appno3=8 then 1 else 0 end
,app9=case when appno1=9 or appno2=9 or appno3=9 then 1 else 0 end
from(
select a.userid,a.business
,appno1=b.appno*a.func1
,appno2=c.appno*a.func2
,appno3=d.appno*a.func3
from table2 a
join(select business,appno from table1 
where funcno=1) b on a.business=b.business
join(select business,appno from table1 
where funcno=2) c on a.business=c.business
join(select business,appno from table1 
where funcno=3) d on a.business=d.business
) a

#8


--下面是数据测试

--测试数据
create table table1(business int,funcno int,appno int)
insert table1
select 1,1,3
union all select 1,2,1 
union all select 1,3,2
union all select 2,1,4
union all select 2,2,5
union all select 2,3,6

create table table2(userid int,business int,func1 int,func2 int,func3 int)

insert table2
select 1,1,1,0,1 
union all select 2,1,0,0,1
union all select 1,2,1,1,1 
union all select 2,2,1,0,0
go

--查询处理
select userid,business
,app1=case when appno1=1 or appno2=1 or appno3=1 then 1 else 0 end
,app2=case when appno1=2 or appno2=2 or appno3=2 then 1 else 0 end
,app3=case when appno1=3 or appno2=3 or appno3=3 then 1 else 0 end
,app4=case when appno1=4 or appno2=4 or appno3=4 then 1 else 0 end
,app5=case when appno1=5 or appno2=5 or appno3=5 then 1 else 0 end
,app6=case when appno1=6 or appno2=6 or appno3=6 then 1 else 0 end
,app7=case when appno1=7 or appno2=7 or appno3=7 then 1 else 0 end
,app8=case when appno1=8 or appno2=8 or appno3=8 then 1 else 0 end
,app9=case when appno1=9 or appno2=9 or appno3=9 then 1 else 0 end
from(
select a.userid,a.business
,appno1=b.appno*a.func1
,appno2=c.appno*a.func2
,appno3=d.appno*a.func3
from table2 a
join(select business,appno from table1 
where funcno=1) b on a.business=b.business
join(select business,appno from table1 
where funcno=2) c on a.business=c.business
join(select business,appno from table1 
where funcno=3) d on a.business=d.business
) a

go
--删除测试环境
drop table table1,table2

/*--测试结果
userid  business  app1  app2  app3  app4  app5  app6  app7  app8  app9
------- --------- ----- ----- ----- ----- ----- ----- ----- ----- ------
1       1         0     1     1     0     0     0     0     0     0
2       1         0     1     0     0     0     0     0     0     0
1       2         0     0     0     1     1     1     0     0     0
2       2         0     0     0     1     0     0     0     0     0

(所影响的行数为 4 行)

--*/