--创建测试环境
create table table1
(
  [公司名称] varchar(10),
  [项目名称] varchar(10),
  [大类]     varchar(10),
  [小类]     varchar(10),
  [金额1]    int,
  [金额2]    int,
  [金额3]    int,
  [日期]     varchar(10),
  [状态]     bit
)
create table table2
(
  [公司名称]  varchar(10),
  [项目名称]  varchar(10),
  [项目收入1] int,
  [项目收入2] int,
  [项目收入3] int,
  [项目支出1] int,
  [项目支出2] int,
  [项目支出3] int,
  [日期]      varchar(10)
)
insert table1
select '公司一','项目一','项目','收入',100,200,250,'200506',0 union
select '公司一','项目一','项目','支出',140,300,200,'200506',0 union
select '公司一','项目一','项目','收入',100,400,null,'200505',0 union
select '公司一','','公司','收入',200,null,400,'200506',0 union
select '公司一','','公司','支出',100,200,null,'200506',0 --测试
insert table2
select [公司名称],
       [项目名称],
       sum(case when [小类]='收入' then [金额1] else 0 end),
       sum(case when [小类]='收入' then [金额2] else 0 end),
       sum(case when [小类]='收入' then [金额3] else 0 end),
       sum(case when [小类]='支出' then [金额1] else 0 end),
       sum(case when [小类]='支出' then [金额2] else 0 end),
       sum(case when [小类]='支出' then [金额3] else 0 end),
       [日期]
from table1
group by [公司名称],[项目名称],[日期]select * from table2--删除测试环境
drop table table1,table2--结果
/*
公司名称       项目名称       项目收入1       项目收入2       项目收入3       项目支出1       项目支出2       项目支出3       日期         
---------- ---------- ----------- ----------- ----------- ----------- ----------- ----------- ---------- 
公司一                   200         0           400         100         200         0           200506
公司一        项目一        100         400         NULL        0           0           0           200505
公司一        项目一        100         200         250         140         300         200         200506(所影响的行数为 3 行)
*/

解决方案 »

  1.   

    试试,没有数据测试:select 公司名称,
    项目名称,
    sum(case when 小类='收入' and 大类='项目' then 金额1 else 0 end) 项目收入1 ,
    sum(case when 小类='收入' and 大类='项目' then 金额2 else 0 end) 项目收入2 ,
    sum(case when 小类='收入' and 大类='项目' then 金额3 else 0 end) 项目收入3 ,
    sum(case when 小类='支出' and 大类='项目' then 金额1 else 0 end) 项目支出1 ,
    sum(case when 小类='支出' and 大类='项目' then 金额2 else 0 end) 项目支出2 ,
    sum(case when 小类='支出' and 大类='项目' then 金额3 else 0 end) 项目支出3 ,
    sum(case when 小类='收入' and 大类='公司' then 金额1 else 0 end) 公司收入1 ,
    sum(case when 小类='收入' and 大类='公司' then 金额2 else 0 end) 公司收入2 ,
    sum(case when 小类='收入' and 大类='公司' then 金额3 else 0 end) 公司收入3 ,
    sum(case when 小类='支出' and 大类='公司' then 金额1 else 0 end) 公司支出1 ,
    sum(case when 小类='支出' and 大类='公司' then 金额2 else 0 end) 公司支出2 ,
    sum(case when 小类='支出' and 大类='公司' then 金额3 else 0 end) 公司支出3 ,
    日期,min(状态) 状态
    from 表一
    group by 公司名称,项目名称,日期
      

  2.   

    phantomMan(asp.net VS 数据库):这个方法好象不行,我一开始就是这样做的
      

  3.   


    insert into 表二
    (公司名称, 项目名称, 日期, 
      项目收入1, 项目收入2,  项目收入3,  项目支出1, 项目支出2, 公司支出1 ..)
    select  公司名称, 项目名称, 日期, 
       sum( case 大类+'-'+小类 when '项目-收入' then 金额1 else 0 end ), 
       sum( case 大类+'-'+小类 when '项目-收入' then 金额2 else 0 end ), 
       sum( case 大类+'-'+小类 when '项目-收入' then 金额3 else 0 end ), 
       sum( case 大类+'-'+小类 when '项目-支出' then 金额1 else 0 end ), 
       sum( case 大类+'-'+小类 when '项目-支出' then 金额2 else 0 end ), 
       sum( case 大类+'-'+小类 when '项目-支出' then 金额3 else 0 end ), 
       sum( case 大类+'-'+小类 when '公司-支出' then 金额1 else 0 end ), 
       sum( case 大类+'-'+小类 when '公司-支出' then 金额2 else 0 end ), 
       sum( case 大类+'-'+小类 when '公司-支出' then 金额3 else 0 end ), 
       ....
    from 表一
    group by 公司名称, 项目名称, 日期