----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-07-24 12:02:21
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
-- May 14 2014 18:34:29 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([name] varchar(1),[count] int,[status] varchar(6))
insert [huang]
select 'A',1,'未完成' union all
select 'B',1,'未完成' union all
select 'C',1,'未完成' union all
select 'D',2,'未完成' union all
select 'A',1,'完成' union all
select 'B',1,'完成' union all
select 'C',1,'完成' union all
select 'D',1,'完成'
--------------开始查询--------------------------select name,COUNT(CASE WHEN [status]='未完成' THEN 1 ELSE NULL END )未完成,
COUNT(CASE WHEN [status]='完成' THEN 1 ELSE NULL END )完成
 from [huang]
 GROUP BY  name 
----------------结果----------------------------
/* 
name 未完成         完成
---- ----------- -----------
A    1           1
B    1           1
C    1           1
D    1           1
*/

解决方案 »

  1.   

    还是说这样:----------------------------------------------------------------
    -- Author  :DBA_HuangZJ(發糞塗牆)
    -- Date    :2014-07-24 12:02:21
    -- Version:
    --      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
    -- May 14 2014 18:34:29 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据:[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([name] varchar(1),[count] int,[status] varchar(6))
    insert [huang]
    select 'A',1,'未完成' union all
    select 'B',1,'未完成' union all
    select 'C',1,'未完成' union all
    select 'D',2,'未完成' union all
    select 'A',1,'完成' union all
    select 'B',1,'完成' union all
    select 'C',1,'完成' union all
    select 'D',1,'完成'
    --------------开始查询--------------------------select name,MAX(CASE WHEN [status]='未完成' THEN [count] ELSE NULL END )未完成,
    MAX(CASE WHEN [status]='完成' THEN [count] ELSE NULL END )完成
     from [huang]
     GROUP BY  name 
    ----------------结果----------------------------
    /* 
    name 未完成         完成
    ---- ----------- -----------
    A    1           1
    B    1           1
    C    1           1
    D    2           1
    */