不明白你的意思
1有AA就没有BB
你查的结果里面怎么1有AA也有BB和

解决方案 »

  1.   


    select a.id,a.aa,b.bb from 表 a left join 表 b on a.id=b.id 
    where a.aa is not null and b.bb is not null
      

  2.   

    想来想去,都是要用临时来解决.--为数据处理准备临时表
    select id,aa,0 as myid into #tb1 from 你的表 where isnull(aa,'')<>''
    select id,bb,0 as myid into #tb2  from 你的表 where isnull(bb,'')<>''--更新临时表,为数据查询做准备
    declare @id int,@myid int
    update #tb1 set @myid=case @id when id then @myid+1 else 1 end
    ,myid=@myid,@id=id
    set @id=nullupdate #tb2 set @myid=case @id when id then @myid+1 else 1 end
    ,myid=@myid,@id=id--从临时表中查询,得到结果
    select a.id,isnull(a.aa,'') as aa,isnull(b.bb,'') as bb
    from #tb1 a full join #tb2 b on a.myid=b.myid and a.id=b.id--删除临时表
    drop table #tb1,#tb2
      

  3.   

    下面是我做的测试--创建数据测试环境
    declare @tb table(ID int,AA varchar(1),BB varchar(1))
    insert into @tb
    select 1,'x',''
    union all select  1,'x',''
    union all select  1,'x',''
    union all select  1,'','x'
    union all select  2,'x',''
    union all select  1,'','x'
    union all select  2,'x',''
    union all select  2,'','x'--为数据处理准备临时表
    select id,aa,0 as myid into #tb1 from @tb where isnull(aa,'')<>''
    select id,bb,0 as myid into #tb2  from @tb where isnull(bb,'')<>''--更新临时表,为数据查询做准备
    declare @id int,@myid int
    update #tb1 set @myid=case @id when id then @myid+1 else 1 end
    ,myid=@myid,@id=id
    set @id=nullupdate #tb2 set @myid=case @id when id then @myid+1 else 1 end
    ,myid=@myid,@id=id--从临时表中查询,得到结果
    select a.id,isnull(a.aa,'') as aa,isnull(b.bb,'') as bb
    from #tb1 a full join #tb2 b on a.myid=b.myid and a.id=b.id--删除临时表
    drop table #tb1,#tb2
      

  4.   

    上面语句的执行结果.id          aa   bb   
    ----------- ---- ---- 
    1           x    x
    1           x    x
    1           x    
    2           x    x
    2           x    (所影响的行数为 5 行)
      

  5.   

    问题的原型是这样的:对于一个考勤表
    姓名ID  迟到时间(包括日期)AA   早退时间(包括日期)BB   (一行代表一次考勤)
    ...         ...........             ............
    ...         ...........             ............
    ...         ...........             ............
    实际上不是每个人每次都 既迟到又早退,所有表中的 AA 和 BB可能只有一项有值
    对表作统计并填充到一个Table中
    为了节约表空间,把他们紧凑第排在一起,迟到一列,早退一列,
    比如,张三 一共迟到 1次,早退3次,则表为:
    ----------------------------------
    张三    迟到     早退
           2月1日    3月1日
                     3月2日
                     3月3日
    -------------------------
    我是想问能不能用select选择出这样的结果来? CrazyFor(太阳下山明朝依旧爬上来) ( ) 的语句,结果行多了,而且该空的地方没有空.不知道有没有可能select出来.