我的想法是先将几个字段合并起来,并过滤掉重复号码.然后在通过单列统计得出结果.可惜对SQL不大熟悉.

解决方案 »

  1.   

    declare @t table(A int,B int,C int,D int)
    insert into @t select 110,110,114,112
    insert into @t select 114,112,111,112
    select
        a.val,
        sum(case b.A when a.val then 1 end) A,
        sum(case b.B when a.val then 1 end) B,
        sum(case b.C when a.val then 1 end) C,
        sum(case b.D when a.val then 1 end) D
    from
        (select A as val from @t union select B from @t union select C from @t union select D from @t) a,
        @t b
    group by
        a.val
    order by
        a.val/*
    110 1 1 NULL NULL
    111 NULL NULL 1 NULL
    112 NULL 1 NULL 2
    114 1 NULL 1 NULL
    */
      

  2.   

    子陌红尘在楼主的基础上加了一列val,这样就是二维关系了.
      

  3.   

    有点东西不大明白,不过不懂那位可以将其转换成适合VFP运行的SQL语句.
      

  4.   

    declare @t1 table(A int,B int,C int,D int)
    insert into @t1 select 110,110,114,112
    insert into @t1 select 114,112,111,112declare @t2 table(A int,ftype char(1))insert into @t2
    select a=A,type='a' from @t1
    union all
    select a=B,type='b' from @t1
    union all
    select a=C,type='C' from @t1
    union all
    select a=D,type='D' from @t1select a=count((case when ftype='a' then 1 else null end )) ,
    b=count((case when ftype='b' then 1 else null end )),
    c=count((case when ftype='c' then 1 else null end )),
    d=count((case when ftype='d' then 1 else null end ))
    from @t2 group by a
      

  5.   

    declare @t1 table(A int,B int,C int,D int)
    insert into @t1 select 110,110,114,112
    insert into @t1 select 114,112,111,112declare @t2 table(A int,ftype char(1))insert into @t2
    select a=A,type='a' from @t1
    union all
    select a=B,type='b' from @t1
    union all
    select a=C,type='C' from @t1
    union all
    select a=D,type='D' from @t1select a,a=count((case when ftype='a' then 1 else null end )) ,
    b=count((case when ftype='b' then 1 else null end )),
    c=count((case when ftype='c' then 1 else null end )),
    d=count((case when ftype='d' then 1 else null end ))
    from @t2 group by a
      

  6.   

    如何将其转换成可以在 Access中运行的语句!?