列1  列2
A    A
A    B
B    B
B    B
B    C
C    C
结果
名称  列1中数量     列2中数量
A     2             1
B     3             3
C     1             2名称从列1中获取
最好sql语句完成,不能分两个视图来获取,因为查询条件是动态的

解决方案 »

  1.   

    select isnull(a.column1,b.column2),c1,c2 from 
    (select column1,count(column1) as c1 from tablename) a 
    full join 
    (select column2,count(column2) as c2 from tablename) b 
    on a.column1=b.column2
      

  2.   

    declare @t table(a char(1),b char(1))
    insert @t select 'A','A'
    union all select 'A','B'
    union all select 'B','B'
    union all select 'B','B'
    union all select 'B','C'
    union all select 'C','C'select a 名称,(select count(1) from @t where a=t.a) 第1列数量,(select count(1) from @t where b=t.a) 第2列数量  from @t t group by a
      

  3.   

    create table test(列1 char(1),  列2 char(10))
    insert test
    select 'A',    'A' union all
    select 'A',    'B'  union all
    select 'B',    'B'  union all
    select 'B',    'B'  union all
    select 'B',    'C'  union all
    select 'C',    'C'select a.*,b.列2中数量 from 
    (select 列1 名称,count(列1) 列1中数量 from test group by 列1) a
    left join
    (select 列2,count(列2) 列2中数量 from test group by 列2)  b
    on a.名称=b.列2drop table test
      

  4.   

    名称   列1中数量       列2中数量       
    ---- ----------- ----------- 
    A    2           1
    B    3           3
    C    1           2
      

  5.   


    declare @t table(a char(1),b char(1))
    insert @t select 'A','A'
    union all select 'A','B'
    union all select 'B','B'
    union all select 'B','B'
    union all select 'B','C'
    union all select 'C','C'select
    名称 = a,
    列1数量 = sum(aa),
    列2数量 = sum(bb)
    from(
    select a, aa=1, bb=0 from @t
    union all
    select b, aa=0, bb=1 from @t
    )a
    group by a
      

  6.   

    if object_id('tempdb..#test') is not null
       drop table #test
    go create table #test([列1] [char](1) , [列2] [char](1))
    insert into #test(列1,列2) values('A','A')
    insert into #test(列1,列2) values('A','B')
    insert into #test(列1,列2) values('B','B')
    insert into #test(列1,列2) values('B','B')
    insert into #test(列1,列2) values('B','C')
    insert into #test(列1,列2) values('C','C')select a.名称,a.列1中数量,b.列2中数量 from
    (select 列1 as '名称' , count(*) as 列1中数量 from #test group by 列1) as a,
    (select 列2 as '名称' , count(*) as 列2中数量 from #test group by 列2) as b
    where a.名称 = b.名称名称   列1中数量       列2中数量       
    ---- ----------- ----------- 
    A    2           1
    B    3           3
    C    1           2(所影响的行数为 3 行)