解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :DBA_HuangZJ(发粪涂墙)
    -- Date    :2014-05-07 14:58:22
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
    -- Apr  2 2010 15:48:46 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([id] nvarchar(4),[weight] int,[count] int,[v1] nvarchar(8),[v2] nvarchar(8))
    insert [huang]
    select 'a',500,10,'a500','a500' union all
    select 'a',200,20,'a','a' union all
    select 'b',500,10,'b500','b500' union all
    select 'b',100,10,'b5','b5' union all
    select NULL,20,20,'20','20' union all
    select NULL,20,20,'20','20'
    --------------生成数据--------------------------select id,MAX([weight])[weight],SUM([count])[count],MAX([v1])[v1],MAX([v2])[v2]
    from [huang]
    WHERE id IS NOT NULL 
    GROUP BY id
    UNION ALL 
    SELECT *
    FROM dbo.huang
    WHERE id IS NULL 
    ----------------结果----------------------------
    /* 
    id   weight      count       v1       v2
    ---- ----------- ----------- -------- --------
    a    500         30          a500     a500
    b    500         20          b500     b500
    NULL 20          20          20       20
    NULL 20          20          20       20
    */