[month]      [wcqk]      
        1           完成
        1           未完成
        1           未完成
        2           完成
        3           未完成
        3           未完成
        4           完成
        。           。
        。           。
        。           。得到distinct month的[wcqk]      [month]      [wcqk]      
        1           部分完成
        2           完成
        3           未完成
        4           完成
        。           。
        。           。

解决方案 »

  1.   

    declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'select distinct a.[month],a.[wcqk] from @t a where not exists(select 1 from @t where [month]=a.[month] and [wcqk]!=a.[wcqk])
    union 
    select a.[month],'部分完成' from @t a where exists(select 1 from @t where [month]=a.[month] and [wcqk]!=a.[wcqk]) group by a.[month]/*
    month       wcqk       
    ----------- ---------- 
    1           部分完成
    2           完成
    3           未完成
    4           完成
    */
      

  2.   

    declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'select distinct
        a.[month],
        [wcqk]=(case (select count(distinct [wcqk]) from @t where [month]=a.[month]) 
                   when 1 then (select distinct [wcqk] from @t where [month]=a.[month]) 
                   else '部分完成'
                end) 
    from 
        @t a/*
    month       wcqk       
    ----------- ---------- 
    1           部分完成
    2           完成
    3           未完成
    4           完成
    */
      

  3.   

    create view  workstatus
    as 
    select distinct month ,(case sum(case wcqk when '完成' then 1 else  0 end) when 0 then '完成' when count(*) then '未完成' else '部分完成' end ) as 完成情况
    from 表 
    where wcqk
    group by month
      

  4.   

    declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'select distinct month ,(case sum(case wcqk when '完成' then 1 else  0 end) when 0 then '完成' when count(*) then '未完成' else '部分完成' end ) as 完成情况
    from @t 
    group by month
      

  5.   

    谢谢楼上的我有一点没说清。
    month       wcqk                 month       wcqk       
    ----------- ----------          ----------- ----------              
    5            完成       ---》    5          完成
    5            完成
    5            完成
      

  6.   

    sorry 写错了declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'select distinct month ,(case sum(case wcqk when '完成' then 1 else  0 end) when 0 then '未完成' when count(*) then '完成' else '部分完成' end ) as 完成情况
    from @t 
    group by month
      

  7.   

    declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'select distinct [month],case when (select top 1 1 from @t where [wcqk]='完成' and  [month]=T.[month]) =1 and (select top 1 1 from @t where [wcqk]='未完成' and  [month]=T.[month]) =1 then '部分完成'
            when (select top 1 1 from @t where [wcqk]='完成' and  [month]=T.[month]) =1 then '完成'
            when (select top 1 1 from @t where [wcqk]='未完成' and  [month]=T.[month])=1 then '未完成' end
    from @t T
      

  8.   


    declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'select distinct ttt.month ,wcqk= '部分完成'  from @t t ,
    (select month from (select * from @t  group by  month ,wcqk)tt group by month  having count(*)>1) ttt
    where t.[month]=ttt.[month]union allselect ttt.month,wcqk from (select month from (select * from @t  group by  month ,wcqk)tt group by month  having count(*)=1)ttt,
    @t t where t.month=ttt.month
      

  9.   

    谢谢楼上的我有一点没说清。
    month       wcqk                 month       wcqk       
    ----------- ----------          ----------- ----------              
    5            完成       ---》    5          完成
    5            完成
    5            完成
    ---------------------------------------------------------------------------------------------------------Any problem?
    declare @t table([month] int,[wcqk] varchar(10))
    insert into @t select 1,'完成'
    insert into @t select 1,'未完成'
    insert into @t select 1,'未完成'
    insert into @t select 2,'完成'
    insert into @t select 3,'未完成'
    insert into @t select 3,'未完成'
    insert into @t select 4,'完成'
    insert into @t select 5,'完成'
    insert into @t select 5,'完成'
    insert into @t select 5,'完成'select distinct a.[month],a.[wcqk] from @t a where not exists(select 1 from @t where [month]=a.[month] and [wcqk]!=a.[wcqk])
    union 
    select a.[month],'部分完成' from @t a where exists(select 1 from @t where [month]=a.[month] and [wcqk]!=a.[wcqk]) group by a.[month]/*
    month       wcqk       
    ----------- ---------- 
    1           部分完成
    2           完成
    3           未完成
    4           完成
    5           完成
    */
    select distinct
        a.[month],
        [wcqk]=(case (select count(distinct [wcqk]) from @t where [month]=a.[month]) 
                   when 1 then (select distinct [wcqk] from @t where [month]=a.[month]) 
                   else '部分完成'
                end) 
    from 
        @t a/*
    month       wcqk       
    ----------- ---------- 
    1           部分完成
    2           完成
    3           未完成
    4           完成
    5           完成
    */