AAA              BBB       CCC
S394353838 281111
S394353838 281113
S394353838 281116
S394546533 222903 088060005
S394546533 222903 083490016
S394546533 222903 083490012
S394546533 222903 083490012统计出AAA,BBB,CCC中每列不重量记录最大的个数,字段为空不统计,
因为AAA中有2,BBB中有4,CCC中3个不重复
所以结果为4,求SQL语句,谢谢

解决方案 »

  1.   

    select count(distinct AAA) AAA,
           count(distinct BBB) BBB,
           count(distinct CCC) CCC
    from tb这样子?
      

  2.   

    select max(c) from
    (
    select count(distinct AAA)c from table_name
    union all
    select count(distinct BBB)c from table_name
    union all
    select count(distinct CCC)c from table_name
    ) t
      

  3.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (AAA varchar(10),BBB int,CCC varchar(9))
    insert into #T
    select 'S394353838',281111,null union all
    select 'S394353838',281113,null union all
    select 'S394353838',281116,null union all
    select 'S394546533',222903,'088060005' union all
    select 'S394546533',222903,'083490016' union all
    select 'S394546533',222903,'083490012' union all
    select 'S394546533',222903,'083490012'select max(c) from
    (
    select count(distinct AAA)c from #T
    union all
    select count(distinct BBB)c from #T
    union all
    select count(distinct CCC)c from #T
    ) t/*
    -----------
    4
    警告: 聚合或其他 SET 操作消除了空值。(1 行受影响)*/
      

  4.   

    declare @T table(AAA varchar(10),BBB int,CCC varchar(9))
    insert into @T
    select 'S394353838',281111,null union all
    select 'S394353838',281113,null union all
    select 'S394353838',281116,null union all
    select 'S394546533',222903,'088060005' union all
    select 'S394546533',222903,'083490016' union all
    select 'S394546533',222903,'083490012' union all
    select 'S394546533',222903,'083490012'select count(distinct AAA) AAA,  
           count(distinct BBB) BBB,  
           count(distinct CCC) CCC  
    from @t  /*
    AAA         BBB         CCC         
    ----------- ----------- ----------- 
    2           4           3(所影响的行数为 1 行)
    */
      

  5.   

      走火,晕
    declare @T table(AAA varchar(10),BBB int,CCC varchar(9))
    insert into @T
    select 'S394353838',281111,null union all
    select 'S394353838',281113,null union all
    select 'S394353838',281116,null union all
    select 'S394546533',222903,'088060005' union all
    select 'S394546533',222903,'083490016' union all
    select 'S394546533',222903,'083490012' union all
    select 'S394546533',222903,'083490012'select top 1 m
    from 
       ( select count(distinct AAA) m from @t union all
         select count(distinct BBB) from @t union all
         select count(distinct CCC) from @t ) a
    order by m desc /*
    m           
    ----------- 
    4(所影响的行数为 1 行)
    */
      

  6.   

    declare @tb table (aaa varchar(20),bbb varchar(20),ccc varchar(20))
    insert into @tb select 'S394353838', '281111',null
    union all select  'S394353838' ,'281113', null
    union all select 'S394353838', '281116' ,null
    union all select 'S394546533' ,'222903', '088060005' 
    union all select 'S394546533' ,'222903' ,'083490016' 
    union all select 'S394546533' ,'222903' ,'083490012' 
    union all select 'S394546533' ,'222903' ,'083490012' 
    select colname, colcount from 
    (select count(*) colcount,'aaa'as colname from (select distinct aaa from @tb where aaa is not null) a
    union all select count(*) ,'bbb' from (select distinct bbb from @tb where bbb is not null) b
    union all select count(*) ,'ccc' from (select distinct ccc from @tb where ccc is not null) c)d
    where colcount in (select max(colcount) from (select count(*) colcount,'aaa'as colname from (select distinct aaa from @tb where aaa is not null) a
    union all select count(*) ,'bbb' from (select distinct bbb from @tb where bbb is not null) b
    union all select count(*) ,'ccc' from (select distinct ccc from @tb where ccc is not null) c)d)
    先贴上再说
      

  7.   

    declare @tb table (aaa varchar(20),bbb varchar(20),ccc varchar(20))
    insert into @tb select 'S394353838', '281111',null
    union all select  'S394353838' ,'281113', null
    union all select 'S394353838', '281116' ,null
    union all select 'S394546533' ,'222903', '088060005' 
    union all select 'S394546533' ,'222903' ,'083490016' 
    union all select 'S394546533' ,'222903' ,'083490012' 
    union all select 'S394546533' ,'222903' ,'083490012' 
    select colname,colcount from 
    (select count(distinct aaa) colcount,'aaa' as colname from @tb where aaa is not null
    union all select count(distinct bbb),'bbb' from @tb where bbb is not null
    union all select count(distinct ccc),'ccc' from @tb where ccc is not null) d
    where colcount =(select max(colcount) from (select count(distinct aaa) colcount,'aaa' as colname from @tb where aaa is not null
    union all select count(distinct bbb),'bbb' from @tb where bbb is not null
    union all select count(distinct ccc),'ccc' from @tb where ccc is not null) d)
    贴完睡觉了