select 进仓个数=sum(case f2 when '进仓' then f3 else 0 end),
       进仓重量=sum(case f2 when '进仓' then f4 else 0 end),
       销售个数=sum(case f2 when '销售' then f3 else 0 end),
       销售重量=sum(case f2 when '销售' then f4 else 0 end),
       出仓个数=sum(case f2 when '出仓' then f3 else 0 end),
       出仓重量=sum(case f2 when '出仓' then f4 else 0 end),
       结存个数=sum(case f2 when '进仓' then f3 else 0 end) - sum(case f2 when '销售' then f3 else 0 end) - sum(case f2 when '出仓' then f3 else 0 end),
       结存重量=sum(case f2 when '进仓' then f4 else 0 end) - sum(case f2 when '销售' then f4 else 0 end) - sum(case f2 when '出仓' then f4 else 0 end)
from t1 group by f1

解决方案 »

  1.   

    create table t1(f1 varchar(5),f2 varchar(10),f3 int,f4 int)
    insert into t1
    select 'A','销售',10,20
    union select 'A','进仓',30,30
    union select 'A','进仓',40,40
    union select 'A','出仓',15,50
    union select 'B','进仓',70,60
    union select 'B','销售',10,70
    union select 'B','销售',20,80
    union select 'B','出仓',10,90-----查询
    select f1,进仓个数=sum(case f2 when '进仓' then f3 else 0 end),
           进仓重量=sum(case f2 when '进仓' then f4 else 0 end),
           销售个数=sum(case f2 when '销售' then f3 else 0 end),
           销售重量=sum(case f2 when '销售' then f4 else 0 end),
           出仓个数=sum(case f2 when '出仓' then f3 else 0 end),
           出仓重量=sum(case f2 when '出仓' then f4 else 0 end),
           结存个数=sum(case f2 when '进仓' then f3 else 0 end) - sum(case f2 when '销售' then f3 else 0 end) - sum(case f2 when '出仓' then f3 else 0 end),
           结存重量=sum(case f2 when '进仓' then f4 else 0 end) - sum(case f2 when '销售' then f4 else 0 end) - sum(case f2 when '出仓' then f4 else 0 end)
    from t1 group by f1----结果
    f1    进仓个数        进仓重量        销售个数        销售重量        出仓个数        出仓重量        结存个数        结存重量        
    ----- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    A     70          70          10          20          15          50          45          0
    B     70          60          30          150         10          90          30          -180(所影响的行数为 2 行)
      

  2.   

    --测试数据
    create table t1 (f1 char, f2 varchar(10), f3 int, f4 int)insert into t1
    select 'A',     '销售',    10,   20 union all
    select 'A',     '进仓',    30,   30 union all
    select 'A',     '进仓',    40,   40 union all
    select 'A',     '出仓',    15,   50 union all
    select 'B',     '进仓',    70 ,  60 union all
    select 'B',     '销售',    10 ,  70 union all
    select 'B',     '销售',    20 ,  80 union all
    select 'B',     '出仓',    10 ,  90--查询
    select *, 进仓个数 - 销售个数 - 出仓个数 as 结存个数,  进仓重量 - 销售重量 - 出仓重量 as 结存重量 from (
        select f1,
            (select sum (f3) from t1 where f1 = T.f1 and f2 = '进仓') as 进仓个数,
            (select sum (f4) from t1 where f1 = T.f1 and f2 = '进仓') as 进仓重量,
            (select sum (f3) from t1 where f1 = T.f1 and f2 = '销售') as 销售个数,
            (select sum (f4) from t1 where f1 = T.f1 and f2 = '销售') as 销售重量,
            (select sum (f3) from t1 where f1 = T.f1 and f2 = '出仓') as 出仓个数,
            (select sum (f4) from t1 where f1 = T.f1 and f2 = '出仓') as 出仓重量
        from t1 T group by f1
    ) B