救命呀,统计所有代理商帐务情况,哪位大哥大姐行行好,拉小弟一把吧
救命呀,统计所有代理商帐务情况,哪位大哥大姐行行好,拉小弟一把吧
统计所有代理商帐务情况(比如查2004-02-03 这一天的帐务情况)
1、 检索条件:时间段
显示信息:帐务流水、业务类型 ,代理商编号,金额统计
库中表名 ACCOUNRES (其中系统编码 和业务流水 是主键)
系统编码 业务类型 帐务流水 代理商编号 时间 金额
hz sl 001 system 2004-02-03 500
hz tk 002 system 2004-02-03 200
sk S1 002 dahang 2004-02-03 600
JK sl 001 system 2004-02-03 100
JK sl 004 dahang 2004-02-03 200
其中 业务类型中 TK 是退款的意思
显示结果为***************:
系统编码 帐务流水、业务类型 ,代理商编号,金额统计
hz 001 sl system 400 (就是500+100-200,200就是退款呀)
hz 002 tk system 400
jk 001 sl system 400
sk 002 sl dahang 800
jk 004 sl dahang 800
主要困惑:有TK(退款)的就要总SUM(JE) 减去 退款 总额,没有TK的就SUM(JE)就行了
由于后台数据库中记录太多,怎么用一条SQL语句解决呀
我只会用几条SQL语句写,影响速度呀,刚进公司2个星期,还是个大菜鸟,
哪位大哥大姐行行好,拉小弟一把吧
问题点数:20、回复次数:9Top
1 楼hdhai9451(☆新人类☆)回复于 2004-09-02 23:34:10 得分 0
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额统计
from ACCOUNRES a left
(select 时间,金额统计=sum((case 业务类型 when tk then 金额统计*(-1) else 金额统计 end))from ACCOUNRES group by 时间)b on a.时间=b.时间Top
2 楼yjdn(人形机器)回复于 2004-09-02 23:34:35 得分 0
create table yw (
系统编码 char (2),
业务类型 char (2),
帐务流水 char (3),
代理商编号 char (10),
时间 char (10),
金额 decimal(12,2) )
insert into yw
select 'hz','sl','001','system','2004-02-03', 500
union
select 'hz', 'tk', '002', 'system' ,'2004-02-03', 200
union
select 'sk', 'sl', '002', 'dahang' ,'2004-02-03', 600
union
select 'JK', 'sl', '001', 'system' ,'2004-02-03' ,100
union
select 'JK', 'sl', '004', 'dahang' ,'2004-02-03', 200
--------------------
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额 as 金额统计
from
yw a,
(select
isnull (a.代理商编号,b.代理商编号) as 代理商编号,
isnull (a.金额,0)-isnull (b.金额,0) as 金额
from
(
select sum(金额) as 金额,代理商编号 from yw where 业务类型='sl'
group by 代理商编号) a full join
(
select sum(金额) as 金额,代理商编号 from yw where 业务类型='tk'
group by 代理商编号) b
on a.代理商编号=b.代理商编号
) b
where a.代理商编号=b.代理商编号Top
3 楼hdhai9451(☆新人类☆)回复于 2004-09-02 23:36:53 得分 10
錯了!改一下
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额统计
from ACCOUNRES a left
(select 代理商编号,金额统计=sum((case 业务类型 when tk then 金额*(-1) else 金额 end))from ACCOUNRES group by 代理商编号)b on a.代理商编号=b.代理商编号
Top
4 楼hdhai9451(☆新人类☆)回复于 2004-09-02 23:39:39 得分 5
暈了!又少打一個單詞
.........
from ACCOUNRES a left join
........
......................Top
5 楼yjdn(人形机器)回复于 2004-09-02 23:40:29 得分 0
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额统计
from yw a left join
(select 代理商编号,金额统计=sum((case 业务类型 when 'tk' then 金额*(-1) else 金额 end))
from yw group by 代理商编号) b on a.代理商编号=b.代理商编号
Top
6 楼yjdn(人形机器)回复于 2004-09-02 23:48:41 得分 0
以上忘加时间,现加上:
1:select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额 as 金额统计
from
yw a,
(select
isnull (a.代理商编号,b.代理商编号) as 代理商编号,
isnull (a.金额,0)-isnull (b.金额,0) as 金额
from
(
select sum(金额) as 金额,代理商编号 from yw where 业务类型='sl' and 时间='2004-02-03'
group by 代理商编号) a full join
(
select sum(金额) as 金额,代理商编号 from yw where 业务类型='tk' and 时间='2004-02-03'
group by 代理商编号) b
on a.代理商编号=b.代理商编号
) b
where a.代理商编号=b.代理商编号
2:hdhai9451的
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额统计
from yw a left join
(select 代理商编号,金额统计=sum((case when 业务类型='tk' and 时间='2004-02-03' then 金额*(-1) else 金额 end))
from yw group by 代理商编号) b on a.代理商编号=b.代理商编号
Top
7 楼zjcxc(邹建)回复于 2004-09-03 08:53:06 得分 0
--查询的存储过程
create proc p_qry
@dt datetime --统计的时间
as
set nocount on
--对查询时间进行处理,保证 时间字段 即使包含了时间信息,也能正确查询出指定日期的数据通信
declare @dt1 datetime,@dt2 datetime
select @dt1=convert(varchar(10),@dt,120)
,@dt2=@dt1+1
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额统计
from ACCOUNRES a,(
select 代理商编号,金额统计=sum(case 业务类型 when 'tk' then -金额 else 金额 end)
from ACCOUNRES
where 时间>=@dt1 and 时间<@dt2
group by 代理商编号
)b where a.代理商编号=b.代理商编号
and a.时间>=@dt1 and a.时间<@dt2
go
--调用实现查询
exec p_qry '2004-02-03'Top
8 楼zjcxc(邹建)回复于 2004-09-03 08:53:43 得分 5
--示例
--示例数据
create table ACCOUNRES(系统编码 varchar(10),业务类型 varchar(10),帐务流水 varchar(10),代理商编号 varchar(10),时间 datetime,金额 int)
insert ACCOUNRES select 'hz','sl','001','system','2004-02-03',500
union all select 'hz','tk','002','system','2004-02-03',200
union all select 'sk','S1','002','dahang','2004-02-03',600
union all select 'JK','sl','001','system','2004-02-03',100
union all select 'JK','sl','004','dahang','2004-02-03',200
go
--查询的存储过程
create proc p_qry
@dt datetime --统计的时间
as
set nocount on
--对查询时间进行处理,保证 时间字段 即使包含了时间信息,也能正确查询出指定日期的数据通信
declare @dt1 datetime,@dt2 datetime
select @dt1=convert(varchar(10),@dt,120)
,@dt2=@dt1+1
select a.系统编码,a.帐务流水,a.业务类型,a.代理商编号,b.金额统计
from ACCOUNRES a,(
select 代理商编号,金额统计=sum(case 业务类型 when 'tk' then -金额 else 金额 end)
from ACCOUNRES
where 时间>=@dt1 and 时间<@dt2
group by 代理商编号
)b where a.代理商编号=b.代理商编号
and a.时间>=@dt1 and a.时间<@dt2
go
--调用实现查询
exec p_qry '2004-02-03'
go
--删除测试
drop table ACCOUNRES
drop proc p_qry
/*--测试结果
系统编码 帐务流水 业务类型 代理商编号 金额统计
---------- ---------- ---------- ---------- -----------
sk 002 S1 dahang 800
JK 004 sl dahang 800
JK 001 sl system 400
hz 001 sl system 400
hz 002 tk system 400
--*/Top
9 楼dahang4251(目前还是菜鸟)回复于 2004-09-03 10:39:45 得分 0
我第一次 在CSDN 上 发帖子,没想到这么快就有这么多的热心的人,感动呀,
Top




