求如下sql语句

aoxiaomin 2007-05-29 08:31:36
数据表1:
类型,名称,数量,金额, 时间
a x1 5 700 2007-01-01
a x2 10 1400 2007-01-02
b x3 6 700 2007-01-02
c x4 9 600 2007-01-03
..........

表2:
金额, 时间
500 2007-01-01
400 2007-01-02
300 2007-01-02
400 2007-01-03
.............


要求生成表3
日期, a数量,a金额,b数,b金,c数,c金,本日合计,本日收款,本日结算
2007-01-01 5 700 0 0 0 0 700 500 200
2007-01-02 10 1400 6 700 0 0 2100 700 1400
2007-01-03 0 0 0 0 9 600 600 400 200
2007-01-04 0 0 0 0 0 0 0 0 0
........
...全文
269 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2007-05-30
  • 打赏
  • 举报
回复
if object_id('pubs..t1') is not null
drop table t1
go
create table t1(类型 varchar(10),名称 varchar(10),数量 int,金额 int,时间 varchar(10))
insert into t1(类型,名称,数量,金额,时间) values('a', 'x1', 5, 700 , '2007-01-01')
insert into t1(类型,名称,数量,金额,时间) values('a', 'x2', 10, 1400 , '2007-01-02')
insert into t1(类型,名称,数量,金额,时间) values('b', 'x3', 6 , 700 , '2007-01-02')
insert into t1(类型,名称,数量,金额,时间) values('c', 'x4', 9 , 600 , '2007-01-03')
go

if object_id('pubs..t2') is not null
drop table t2
go
create table t2(金额 int,时间 varchar(10))
insert into t2(金额,时间) values(500, '2007-01-01')
insert into t2(金额,时间) values(400, '2007-01-02')
insert into t2(金额,时间) values(300, '2007-01-02')
insert into t2(金额,时间) values(400, '2007-01-03')
go

declare @sql varchar(8000)
set @sql = 'select t.* ,m.金额 本日收款 , t.本日合计 - m.金额 本日结算 from (select 时间'
select @sql = @sql + ' , sum(case 类型 when ''' + 类型 + ''' then 数量 else 0 end) [' + 类型 + '数量]'
+ ' , sum(case 类型 when ''' + 类型 + ''' then 金额 else 0 end) [' + 类型 + '金额]'
from (select distinct 类型 from t1) as a
set @sql = @sql + ' ,sum(金额) 本日合计 from t1 group by 时间) t,(select 时间 , sum(金额) 金额 from t2 group by 时间) m where t.时间 = m.时间'
exec(@sql)

drop table t1,t2

/*
时间 a数量 a金额 b数量 b金额 c数量 c金额 本日合计 本日收款 本日结算
---------- ----- ----- ----- ----- ----- ----- -------- -------- -----------
2007-01-01 5 700 0 0 0 0 700 500 200
2007-01-02 10 1400 6 700 0 0 2100 700 1400
2007-01-03 0 0 0 0 9 600 600 400 200
*/
zhaoanle 2007-05-29
  • 打赏
  • 举报
回复


--测试表
create table 表1(类型 varchar(10),名称 varchar(10),数量 int,金额 int,时间 datetime)
create table 表2(金额 int,时间 datetime)

insert 表1 select 'a', 'x1', 5, 700 , '2007-01-01'
union all
select 'a' , 'x2' , 10 , 1400 , '2007-01-02'
union all
select 'b' , 'x3' , 6 , 700 , '2007-01-02'
union all
select 'c' , 'x4' , 9 , 600 , '2007-01-03'
union all
select 'd','x1',5,500,'2007-01-04'

insert 表2 select 500 , '2007-01-01'
insert 表2 select 400 , '2007-01-02'
insert 表2 select 300 , '2007-01-02'
insert 表2 select 400 , '2007-01-03'

--查询

declare @sql1 varchar(8000),@sql2 varchar(8000)
select @sql1='select 时间'
select @sql1=@sql1+','+类型+'数量=sum(case when 类型='''+类型+''' then 数量 else 0 end),'
+类型+'金额=sum(case when 类型='''+ 类型+''' then 金额 else 0 end)'
from (select distinct 类型 from 表1) a

select @sql2=',本日合计=(select sum(金额) from 表1 where 时间=a.时间 group by 时间),
本日收款=isnull((select sum(金额) from 表2 where 时间=a.时间 group by 时间),0),
本日结算=(select sum(金额) from 表1 where 时间=a.时间 group by 时间)-
isnull((select sum(金额) from 表2 where 时间=a.时间 group by 时间),0)
from 表1 a group by 时间'

exec(@sql1+@sql2)

--删除测试表
drop table 表1,表2


/*结果
时间 a数量 a金额 b数量 b金额 c数量 c金额 d数量 d金额 本日合计 本日收款 本日结算
----------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2007-01-01 00:00:00.000 5 700 0 0 0 0 0 0 700 500 200
2007-01-02 00:00:00.000 10 1400 6 700 0 0 0 0 2100 700 1400
2007-01-03 00:00:00.000 0 0 0 0 9 600 0 0 600 400 200
2007-01-04 00:00:00.000 0 0 0 0 0 0 5 500 500 0 500



*/



中国风 2007-05-29
  • 打赏
  • 举报
回复
没测试过:
表ta:
类型,名称,数量,金额, 时间
a x1 5 700 2007-01-01
a x2 10 1400 2007-01-02
b x3 6 700 2007-01-02
c x4 9 600 2007-01-03

declare @s varchar(4000)
set @s=''
select @s=@s+','+quotename(类型+'数量')+'=sum(case when 类型='+quotename(类型,'''')+
' then 数量 else 0 end),'+
quotename(类型+'金额')+'=sum(case when 类型='+quotename(类型,'''')+
' then 金额 else 0 end)'
from ta
group by 类型

set @s='select [日期]=convert(varchar(10),时间,120) '+@s+' from ta group by convert(varchar(10),时间,120)'

exec(@s)
aoxiaomin 2007-05-29
  • 打赏
  • 举报
回复
目前采用了按照查询时间范围先把范围内日期插入表3,每天一条记录。然后动态生成表3的科变字段a数量,a金额,b数量,b金额等。再最后用循环方式对每条记录修改内容产生表3最终结果。
速度慢了点。。请问有更快的实现方法吗?
(delphi+sql环境)

34,590

社区成员

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

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