接此贴http://topic.csdn.net/u/20081209/08/b457255b-654f-487e-8867-c94977844ccd.html谢谢,谢谢高人。 如果可以,我想问一下,如果从另一个表中连接查询CA的所有数据,该怎么实现呢? 
加个表Test2 
类名 列号 行号 数据A 数据B 
CA         5 1 1          11         
CA         5 2 2          12         
CA         5 3 3          13         
CA         5 4 4          213        
CA         6 1 4          123        
CA         6 2 3          123        
CA         6 3 2          123        
CA         6 4 1          123        
CB         7 1 53         215        
CB         7 2 99         123        
CB         7 3 454        123        
CB         8 1 43534      123        
CB         8 2 99         12         
CB         8 3 99         23         
CB         9 1 99         123        
CB         9 2 99         77 
CB         9 3 543        12         
CB         10 1 34         31         
CB         10 2 99         23         
CB         10 3 34         12     希望查出CA的所有数据 
CA 
列1数据1 列2数据2 列5数据A 列5数据B 列6数据A 列6数据B 
2343 234 1     11 4    123 
99 99 2      12 3   123 
23432 5 3  13 2    123 
99 45 4   213 1    123 
希望高人不会觉得我烦。 
再次感谢各位的回帖!!!     

解决方案 »

  1.   


    静态语句
    select 行号,列1数据1=max(case 列号 when 1 then 数据 end),列2数据2=max(case 列号 when 2 then 数据 end),
           列5数据A=max(case  列号2 when 5 then 数据A end),列5数据B=max(case  列号2 when 5 then 数据B end),
           列6数据A=max(case  列号2 when 6 then 数据A end),列5数据B=max(case  列号2 when 6 then 数据B end)   
    from (select tb1.*,tb2.列号 as 列号2,tb2.行号 as 行号2,数据A,数据B  from tb1,tb2 where tb1.类号=tb2.类名 and tb1.类号='ca' and tb1.行号=tb2.行号) K
    group by 行号,行号21 2343 234 1 11 4 123
    2 99 99 2 12 3 123
    3 23432 5 3 13 2 123
    4 99 45 4 213 1 123
      

  2.   

    结果为空我使用这条也是
    select * from Test1,Test2  where Test1.类号=Test2.类号不知道为什么 
      

  3.   

    create table tb1(类号 varchar(10) , 列号 int, 行号 int, 数据 int) 
    insert into tb1 values('CA', 1, 1 ,2343 )
    insert into tb1 values('CA', 1, 2 ,99 )
    insert into tb1 values('CA', 1, 3 ,23432) 
    insert into tb1 values('CA', 1, 4 ,99 )
    insert into tb1 values('CA', 2, 1 ,234 )
    insert into tb1 values('CA', 2, 2 ,99 )
    insert into tb1 values('CA', 2, 3 ,5 )
    insert into tb1 values('CA', 2, 4 ,45 )
    insert into tb1 values('CB', 1, 1 ,53 )
    insert into tb1 values('CB', 1, 2 ,99 )
    insert into tb1 values('CB', 1, 3 ,454 )
    insert into tb1 values('CB', 2, 1 ,43534) 
    insert into tb1 values('CB', 2, 2 ,99 )
    insert into tb1 values('CB', 2, 3 ,99 )
    insert into tb1 values('CB', 3, 1 ,99 )
    insert into tb1 values('CB', 3, 2 ,99 )
    insert into tb1 values('CB', 3, 3 ,543 )
    insert into tb1 values('CB', 4, 1 ,34 )
    insert into tb1 values('CB', 4, 2 ,99 )
    insert into tb1 values('CB', 4, 3 ,34 )
    create table tb2(类号 varchar(10) , 列号 int , 行号 int , 数据A int , 数据B int) 
    insert into tb2 values('CA' , 5 ,1 ,1 , 11 ) 
    insert into tb2 values('CA' , 5 ,2 ,2 , 12 ) 
    insert into tb2 values('CA' , 5 ,3 ,3 , 13 ) 
    insert into tb2 values('CA' , 5 ,4 ,4 , 213) 
    insert into tb2 values('CA' , 6 ,1 ,4 , 123) 
    insert into tb2 values('CA' , 6 ,2 ,3 , 123) 
    insert into tb2 values('CA' , 6 ,3 ,2 , 123) 
    insert into tb2 values('CA' , 6 ,4 ,1 , 123) 
    insert into tb2 values('CB' , 7 ,1 ,53, 215 ) 
    go
    --sql 2000 静态方法1
    select a.行号,
      max(case a.列号 when 1 then a.数据 else 0 end) '列1数据1',
      max(case a.列号 when 2 then a.数据 else 0 end) '列2数据2',
      max(case b.列号 when 5 then b.数据A else 0 end) '列5数据A',
      max(case b.列号 when 5 then b.数据B else 0 end) '列5数据B',
      max(case b.列号 when 6 then b.数据A else 0 end) '列6数据A',
      max(case b.列号 when 6 then b.数据B else 0 end) '列6数据B'
    from tb1 a , tb2 b
    where a.类号 = 'CA' and b.类号 = 'CA' and a.行号 = b.行号
    group by a.行号
    /*
    行号          列1数据1       列2数据2       列5数据A       列5数据B       列6数据A       列6数据B       
    ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    1           2343        234         1           11          4           123
    2           99          99          2           12          3           123
    3           23432       5           3           13          2           123
    4           99          45          4           213         1           123(所影响的行数为 4 行)
    */--sql 2000 静态方法2
    select 行号,
      max(case 列号 when 1 then 数据A else 0 end) '列1数据1',
      max(case 列号 when 2 then 数据A else 0 end) '列2数据2',
      max(case 列号 when 5 then 数据A else 0 end) '列5数据A',
      max(case 列号 when 5 then 数据B else 0 end) '列5数据B',
      max(case 列号 when 6 then 数据A else 0 end) '列6数据A',
      max(case 列号 when 6 then 数据B else 0 end) '列6数据B'
    from
    (
      select 类号 , 列号 , 行号 , 数据 数据A , 数据B = 0 from tb1 where 类号 = 'CA' union all select * from tb2 where 类号 = 'CA'
    ) t
    group by 行号
    /*
    行号          列1数据1       列2数据2       列5数据A       列5数据B       列6数据A       列6数据B       
    ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    1           2343        234         1           11          4           123
    2           99          99          2           12          3           123
    3           23432       5           3           13          2           123
    4           99          45          4           213         1           123(所影响的行数为 4 行)
    */--sql 2000动态 
    declare @sql varchar(8000)
    set @sql = 'select 行号 '
    select @sql = @sql + ' , max(case 列号 when ''' + cast(列号 as varchar) + ''' then 数据A else 0 end) [列' + cast(列号 as varchar) + '数据A]'
                       + ' , max(case 列号 when ''' + cast(列号 as varchar) + ''' then 数据B else 0 end) [列' + cast(列号 as varchar) + '数据B]'
    from (select distinct 列号 from (select 类号 , 列号 , 行号 , 数据 数据A , 数据B = 0 from tb1 where 类号 = 'CA' union all select * from tb2 where 类号 = 'CA') t) as a
    set @sql = @sql + ' from (select 类号 , 列号 , 行号 , 数据 数据A , 数据B = 0 from tb1 where 类号 = ''CA'' union all select * from tb2 where 类号 = ''CA'')t group by 行号'
    exec(@sql) 
    /*
    行号          列1数据A       列1数据B       列2数据A       列2数据B       列5数据A       列5数据B       列6数据A       列6数据B       
    ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    1           2343        0           234         0           1           11          4           123
    2           99          0           99          0           2           12          3           123
    3           23432       0           5           0           3           13          2           123
    4           99          0           45          0           4           213         1           123
    */drop table tb1 , tb2