y一个SQL问题(是sql高手的看看)

来源:百度知道 编辑:UC知道 时间:2024/07/08 10:41:40
銄袏鯐__. 09:27:56
这样的一张表(见图),其中dep_id代表代理商的代号,Channels_Name代表代理商的名称,其中OperType代表操作的类型(0代表缴纳,1代表支取退还),Amount代表操作的金额。

需求是输入一段时间如:2009-02-25,2009-02-27查出以下这样一张表

代理商名称 编码 缴纳 支取 最后金额
Dld 321 1100 100 1000
Dld2 322 1000 300 700

各位大哥大姐用存储过程如何实现?(最好不要用游标)
銄袏鯐__. 09:27:56
这样的一张表,其中dep_id代表代理商的代号,Channels_Name代表代理商的名称,其中OperType代表操作的类型(0代表缴纳,1代表支取退还),Amount代表操作的金额。

需求是输入一段时间如:2009-02-25,2009-02-27查出以下这样一张表

代理商名称 编码 缴纳 支取 最后金额
Dld 321 1100 100 1000
Dld2 322 1000 300 700

各位大哥大姐用存储过程如何实现?(最好不要用游标)
銄袏鯐__. 09:27:56
这样的一张表,其中dep_id代表代理商的代号,Channels_Name代表代理商的名称,其中OperType代表操作的类型(0代表缴纳,1代表支取退还),Amount代表操作的金额。

需求是输入一段时间如:2009-02-25,2009-02-27查出以下这样一张表

代理商名称 编码 缴纳 支取 最后金额
Dld 321 1100 100 1000
Dld2 322 1000 300 700

各位大哥大姐用存储过程如何实现?(最好不要用游标)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AgentAcct_History]') and

不需要用存储过程的。
select a.Channels_Name ,a.dep_id ,
(select sum(case when OperType = '0' then amount else 0 - amount end)
from AgentAcct_History
where Oper_datetime < 日期条件1
and dep_id = a.dep_id) as amount_begin,
sum(isnull(a.amount_in,0)) as amount_in,
sum(isnull(a.amount_out,0)) as amount_out,
sum(isnull(a.amount_in,0)) - sum(isnull(a.amount_out,0)) as amount_last
from
(select Channels_Name ,dep_id ,
case when OperType = '0' then Amount else 0 end as Amount_in,
case when OperType = '1' then Amount else 0 end as Amount_out
from AgentAcct_History
where Oper_datetime between 日期条件1 and 日期条件2
) a
group by a.Channels_Name ,a.dep_id

select * from table21313 where date213213 between '2009-02-25' and '2009-02-27'