你们也可到那个标题为"特定数据集显示"的贴子里去接分,因为题目取错了,所以又开了个贴,呵呵.谢谢大家
字段名1  字段名2
 a1         0
 a2         0
 a3         0
 b1         1
 b2         1怎样用sql检索成下面的格式,字段为0的归一列,为1的归为别一列?
a1  b1
a2  b2
a3  null

解决方案 »

  1.   

    SELECT MAX(CASE 字段名2 WHEN 0 THEN 字段名1 ELSE '' END) AS 字段1
    MAX(CASE 字段名2 WHEN 1 THEN 字段名1 ELSE '' END) AS 字段2
    FROM TABLENAME
      

  2.   

    掉了个逗号,
    SELECT MAX(CASE 字段名2 WHEN 0 THEN 字段名1 ELSE '' END) AS 字段1,
    MAX(CASE 字段名2 WHEN 1 THEN 字段名1 ELSE '' END) AS 字段2
    FROM TABLENAME
      

  3.   

    create table t 
    (
    t1  varchar(10),
    t2 int ,
    )insert into t select 'a1',0
    insert into t select 'a2',0
    insert into t select 'a3',0
    insert into t select 'b1',1
    insert into t select 'b2',1
    select t1,22 as t2 ,identity(int,1,1 ) id into #t1 from  t where t2= 0 
    select t1,22 as t2 ,identity(int,1,1 ) id into #t2 from  t where t2= 1select a.t1 ,b.t1  
    from #t1 a left join #t2 b   on a.id =b.id t1         t1         
    ---------- ---------- 
    a1         b1
    a2         b2
    a3         NULL(所影响的行数为 3 行)
      

  4.   

    重复帖?create table tb
    (
    字段名1 char(2),
    字段名2 int
    )
    insert into tb(字段名1,字段名2) values('a1',0)
    insert into tb(字段名1,字段名2) values('a2',0)
    insert into tb(字段名1,字段名2) values('a3',0)
    insert into tb(字段名1,字段名2) values('b1',1)
    insert into tb(字段名1,字段名2) values('b2',1)
    goselect id=identity(int,1,1) , 字段名1 into test1 from tb where 字段名2 = 0
    select id=identity(int,1,1) , 字段名1 into test2 from tb where 字段名2 = 1select test1.字段名1 as A , test2.字段名1 as B
    from test1 left join test2 on test1.id = test2.iddrop table tb
    drop table test1
    drop table test2A    B    
    ---- ---- 
    a1   b1
    a2   b2
    a3   NULL(所影响的行数为 3 行)
      

  5.   

    用一个sql不行吗,还要定义这个
    select id=identity(int,1,1) , 字段名1 into test1 from tb where 字段名2 = 0
    select id=identity(int,1,1) , 字段名1 into test2 from tb where 字段名2 = 1
    多个sql,我想要一个sql的,因为写在程序里
      

  6.   

    挺复杂的。 不过,程序里面应该也可以执行多条 SQL 吧
      

  7.   

    萤火虫的方法好,但查询出来是这样子的
    a1  
    a2  
    a3  
        b1
        b2
        null我想要的是这样的,能有别的办法吗,谢谢
    a1  b1
    a2  b2
    a3  null