phantomMan(全力转向 .net 和 数据库方向) :你在吗,能不能帮帮我看看这个sql怎么写

windy_wzh 2005-07-29 11:38:36
表一:
公司名称 项目名称 大类 小类 金额1 金额2 金额3 日期 状态
公司一 项目一 项目 收入 100 200 250 200506 0
公司一 项目一 项目 支出 140 300 200 200506 1
公司一 项目一 项目 收入 100 400 200505 0
公司一 公司 收入 200 400 200506 0
公司一 公司 支出 100 200 200506 0

要做的工作,把表一中公司名称、项目名称、日期相同的记录的项目收入、项目支出、公司收入、公司支出中的金额1,金额2,金额3...合并为一条记录,放入表二中。

表二:

公司名称 项目名称 项目收入1 项目收入2 项目收入3 项目支出1 项目支出2 公司支出1 ..日期
公司一 目一 100 200 250 140 300 0 ..200506
公司一 0 0 0 0 100 ..200506
请问这条sql语句怎么写,谢谢!
...全文
140 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
phantomMan 2005-07-29
  • 打赏
  • 举报
回复
公司名称 项目名称 项目收入1 项目收入2 项目收入3 项目支出1 项目支出2 公司支出1 ..日期

---------------------------------------------
上面的列是固定的吗???
yyd415 2005-07-29
  • 打赏
  • 举报
回复
顶!
山林73 2005-07-29
  • 打赏
  • 举报
回复

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 公司名称, 项目名称, 日期
windy_wzh 2005-07-29
  • 打赏
  • 举报
回复
phantomMan(asp.net VS 数据库):这个方法好象不行,我一开始就是这样做的
phantomMan 2005-07-29
  • 打赏
  • 举报
回复
试试,没有数据测试:

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 公司名称,项目名称,日期
vivianfdlpw 2005-07-29
  • 打赏
  • 举报
回复
--创建测试环境
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 行)
*/
windy_wzh 2005-07-29
  • 打赏
  • 举报
回复
对,是固定的
windy_wzh 2005-07-29
  • 打赏
  • 举报
回复
有谁知道吗?各位高手帮帮我

34,592

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧