测试数据  806 2944 618 2962
 813 3313 711 3319
 715 3260 711 3260
 712 3241 701 3238
 706 2874 809 2891
 707 2599 618 2599
 705 2635 712 2647
 814 2020 606 2034
 715 2107 624 2086
第一列是工号 第二列是工作量1第三列也是工号第四列是工作量2求工作量总和格式工号 工作量1总和 工作量2总和

解决方案 »

  1.   


    select
      col1 as 工号,sum(col2) as 工作量1总和,sum(col4) as 工作量2总和
    from
    (select col1,col2,col4 from tb
    union 
    select col3,col2,col4 from tb)t
    group  by
       col1
      

  2.   

    select 工号,sum(工作量1) as 工作量1,sum(工作量2) as 工作量2
    from (
    select 工号1 as 工号,工作量1,cast(0 as numeric(18,4) 0 as 工作量2
    from tab
    union all
    select 工号2 as 工号,0 as 工作量1,工作量2
    from tab
    ) as t
    group by 工号
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-10-09 14:55:34
    -- Verstion:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    go 
    create table [TB]([COL1] int,[COL2] int,[COL3] int,[COL4] int)
    insert [TB]
    select 806,2944,618,2962 union all
    select 813,3313,711,3319 union all
    select 715,3260,711,3260 union all
    select 712,3241,701,3238 union all
    select 706,2874,809,2891 union all
    select 707,2599,618,2599 union all
    select 705,2635,712,2647 union all
    select 814,2020,606,2034 union all
    select 715,2107,624,2086
    --------------开始查询--------------------------
    select
      col1 as 工号,sum(col2) as 工作量1总和,sum(col4) as 工作量2总和
    from
    (select col1,col2,col4 from tb
    union all
    select col3,col2,col4 from tb)t
    group  by
       col1
    ----------------结果----------------------------
    /* 工号          工作量1总和      工作量2总和
    ----------- ----------- -----------
    606         2020        2034
    618         5543        5561
    624         2107        2086
    701         3241        3238
    705         2635        2647
    706         2874        2891
    707         2599        2599
    711         6573        6579
    712         5876        5885
    715         5367        5346
    806         2944        2962
    809         2874        2891
    813         3313        3319
    814         2020        2034(14 行受影响)*/
      

  4.   

    create table tb(id1 int,w1 int,id2 int,w2 int)
    insert into tb select 806,2944,618,2962
    insert into tb select 813,3313,711,3319
    insert into tb select 715,3260,711,3260
    insert into tb select 712,3241,701,3238
    insert into tb select 706,2874,809,2891
    insert into tb select 707,2599,618,2599
    insert into tb select 705,2635,712,2647
    insert into tb select 814,2020,606,2034
    insert into tb select 715,2107,624,2086
    go
    select id1 as 工号,SUM(w1) as 工作量1总和,SUM(w2) as 工作量2总和 from(
    select id1,w1,0 w2 from tb
    union all
    select id2,0,w2 from tb
    )t group by id1
    /*
    工号          工作量1总和      工作量2总和
    ----------- ----------- -----------
    606         0           2034
    618         0           5561
    624         0           2086
    701         0           3238
    705         2635        0
    706         2874        0
    707         2599        0
    711         0           6579
    712         3241        2647
    715         5367        0
    806         2944        0
    809         0           2891
    813         3313        0
    814         2020        0(14 行受影响)
    */
    go
    drop table tb