读书人

这个报表怎么生成

发布时间: 2012-01-09 21:05:42 作者: rapoo

这个报表如何生成?
表一(t1):
id f1 f2 f3
01 hh gg jj
02 kk gt ft

表二(t2)
id fd1 fd2 fd3
01 jjh kkk hhh
01 juh hhh ghj
02 kkh bbn hjb
02 hhg tty ghb

表三(t3)
id fed1 fed2 fed3
01 jkk mjkj kkkk
01 kkk llll lllk
02 jjm jkjjk kjkj
02 jjj jkjkk kjk
02 nbjh jk1 89j

需要生成的表为
report_1
id f1 f2 f3 fd1 fd2 fd3 fed1 fed2 fed3
01 hh gg jj
jjh kkk hhh
kkh bbn hjb
jkk mjkj kkkk
kkk llll lllk
02 kk gt ft

kkh bbn hjb
hhg tty ghb
jjm jkjjk kjkj
jjj jkjkk kjk
nbjh jk1 89j

请给出实现的代码,谢谢!!



[解决办法]

SQL code
if exists(select * from sysobjects where name in('t1','t2','t3')) 
drop table t1,t2,t3
create table t1(id integer ,f1 varchar(10),f2 varchar(10), f3 varchar(10))
create table t2(id integer ,fd1 varchar(10),fd2 varchar(10), fd3 varchar(10))
create table t3(id integer ,fed1 varchar(10),fed2 varchar(10), fed3 varchar(10))

insert into t1(id ,f1,f2,f3)
select 01, 'hh', 'gg', 'jj'
union select
02, 'kk', 'gt', 'ft'

insert into t2(id ,fd1,fd2,fd3)
select 01, 'hh', 'gg', 'jj'
union select
02, 'kk', 'gt', 'ft'


insert into t3(id ,fed1,fed2,fed3)
select 01, 'hh', 'gg', 'jj'
union select
02, 'kk', 'gt', 'ft'


if exists(select * from sysobjects where name='t')
drop table t
create table t(id integer ,c1 varchar(50),c2 varchar(50),c3 varchar(50),falg varchar(1))
insert into t(id,c1,c2,c3,falg)
select id,f1,f2,f3,'1' from t1
insert into t(id,c1,c2,c3,falg)
select id,' '+fd1,fd2,+fd3,'2' from t2
insert into t(id,c1,c2,c3,falg)
select id,' '+fed1,fed2,fed3,'3' from t3

select case when falg='1' then id else '' end,c1,c2,c3
from t order by id,falg
drop table t

读书人网 >SQL Server

热点推荐