[month] [wcqk]
1 完成
1 未完成
1 未完成
2 完成
3 未完成
3 未完成
4 完成
。 。
。 。
。 。得到distinct month的[wcqk] [month] [wcqk]
1 部分完成
2 完成
3 未完成
4 完成
。 。
。 。
1 完成
1 未完成
1 未完成
2 完成
3 未完成
3 未完成
4 完成
。 。
。 。
。 。得到distinct month的[wcqk] [month] [wcqk]
1 部分完成
2 完成
3 未完成
4 完成
。 。
。 。
解决方案 »
- 请教一下SQL高手
- 菜鸟的问题:这个SQL语句怎么写,在线等
- SQLDMO 获得指定数据库中的所有表
- 如何恢复被识误删的表?
- 小问题,请高手帮助
- 请帮忙看一下我的函数有什么问题。
- 求助:用vc++和sql sever 开发软件,在数据库中可以实现的查询功能在vc++平台上都能实现吗??
- BCP数据导入的问题-如何处理带单引号的字段数据和NULL值的数据
- 请问如何管理job scheduling……
- 跪求高手帮忙用SQL语句展BOM
- MySql 里怎么完成批量替换哪?
- Sum 函数的执行出错,提示“Arithmetic overflow error converting expression to data type int.”,怎么解决?
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 完成
*/
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 完成
*/
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
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
month wcqk month wcqk
----------- ---------- ----------- ----------
5 完成 ---》 5 完成
5 完成
5 完成
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
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
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
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 完成
*/