select 年月,种类,sum(case 出入 when '出' then 出量 else 0 end) 出量,代号,sum(case 出入 when '出' then 进量 else 0 end) 进量,人员 from 表A group by 年月,种类,代号,人员

解决方案 »

  1.   

    select 年月,种类,sum(case 出入 when '出' then 出量 else 0 end) 出量,代号,sum(case 出入 when '入' then 进量 else 0 end) 进量,人员 from 表A group by 年月,种类,代号,人员
      

  2.   

    是这个意思吗?
     
     Select a.年月,a.种类,a.出量,a.代号,b.进量,b.人员
     from (select max(出量) as 出量,种类,代号 
          from table group by 种类,代号) as a
       inner join (select min(进量) as 进量,代号,人员
         from table group by 代号,人员) as b
       on a.代号=b.代号
      

  3.   

    --建立测试表
    create table 表A(年月 varchar(100),种类 varchar(100),出量 int,代号 varchar(100),出入 varchar(100),进量 int,人员 varchar(100))
    insert into 表A select '200408','小麦',6000,'001','出',6000,'张三'
    union all select '200408','大麦',3000,'002','出',3000,'李四'
    union all select '200408','小麦',2500,'001','入',2500,'张三'
    union all select '200408','大麦',5000,'002','入',5000,'李四'
    union all select '200408','小麦',1500,'003','入',1500,'张三'--查询数据
    select 年月,种类,sum(case 出入 when '出' then 出量 else 0 end) 出量,代号,sum(case 出入 when '入' then 进量 else 0 end) 进量,人员 from 表A group by 年月,种类,代号,人员--删除测试表
    drop table 表A
    --查询结果
    年月         种类         出量          代号         进量          人员         
    ---------- ---------- ----------- ---------- ----------- ---------- 
    200408     大麦         3000        002        5000        李四
    200408     小麦         6000        001        2500        张三
    200408     小麦         0           003        1500        张三