treeview结构数据汇总 |
答題得分者是:Mickey
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
公司資料0
------公司001
----------公司00100
---------------公司0010000
---------------公司0010001
------公司002
----------公司00200
---------------公司0020000
---------------公司0020001
----------------------公司002000100
----------公司00201
《公司表》 的树结构如上(公司后的数字为公司代码)
每个公司都有收入和支出 现在想计算出每个公司的盈利数字
每个公司的利润为 (自身收入-自身支出)+子公司的利润(子公司收入-子公司支出) 公司的业绩表现用 《总汇表》体现
我想通过查询出所有的非叶子节点 通过游标循环对每个非叶子节点进行汇总
我写了一个存储过程 如下
create proc zonghui @time int as begin tran declare @code varchar(20) ---------我觉得应该通过这一句来查询出所有的非叶子节点 但在下.... declare code cursor for select 代码 from 公司表... open code fetch code into @code while @@fetch_status=-2 select @code=rtrim(@code) '%' delete from 总汇表 where 时间=@time insert into 总汇表(时间) values(@time) update 总汇表 set 利润=b.利润 from 总汇表 as a,(select sum(isnull(收入)-isnull(支出)) as 利润 from 公司表 where 时间=@time and 公司代码like @code) as b where a.时间=@time close code deallocate code commit go若按如上存储过程计算《总汇表》的公司利润 结果是所有公司利润为零 |
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
请问 Mickey 公司資料0
------公司001
----------公司00100
---------------公司0010000
---------------公司0010001
------公司002
----------公司00200
---------------公司0020000
---------------公司0020001
----------------------公司002000100
----------公司00201 如果以上面这个结构为例 具体怎么通过遞回(Recursive)来一層一層呼叫下去...
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
以下只是個遞迴程序的例子, 參考參考 :
create procedure TBLTPRCF @parent varchar(20),@tbltp varchar(1) /* Base On SCHEMA_RELS Recursive Update SCHEMA_TBLS.TBL_TYPE */ as begin declare @child varchar(20) UPDATE SCHEMA_TBLS set TBL_TYPE=@tbltp where rtrim(TBL_NAME)=rtrim(@parent) declare child_tbls scroll cursor for select distinct rtrim(CHILD) from SCHEMA_RELS r where rtrim(PARENT)=rtrim(@parent) and ISIDEN='Yes' and rtrim(CHILD)<>rtrim(@parent) and (select TBL_TYPE from SCHEMA_TBLS where TBL_NAME=r.CHILD)<>@tbltp order by rtrim(CHILD) open child_tbls fetch child_tbls into @child while (@@fetch_status=0) begin close child_tbls deallocate child_tbls exec TBLTPRCF @child,@tbltp declare child_tbls scroll cursor for select distinct rtrim(CHILD) from SCHEMA_RELS r where rtrim(PARENT)=rtrim(@parent) and ISIDEN='Yes' and rtrim(CHILD)<>rtrim(@parent) and (select TBL_TYPE from SCHEMA_TBLS where TBL_NAME=r.CHILD)<>@tbltp order by rtrim(CHILD) open child_tbls fetch child_tbls into @child end close child_tbls deallocate child_tbls end |
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
不好意思 耽误了好几天 SORRY 我仔细看了一下Mickey给出的例子 我想请问
/* Base On SCHEMA_RELS Recursive Update SCHEMA_TBLS.TBL_TYPE */
中 SCHEMA_RELS 和SCHEMA_TBLS的表结构是怎样的 再者 自我感觉上 不好将其与treeview结构的数据汇总结合起来 此例关键在于使用“ exec TBLTPRCF @child,@tbltp” 体现循环(在下水平有限 望大大见谅)
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
Mickey大大 你好
可能是我一直没有从
“declare code cursor for select 代码 from 公司表 where ...
open code
fetch code into @code while @@fetch_status=-2
select @code=rtrim(@code) '%'”的模式中走出来 反复试验还是无效 很遗憾 在《公司表》中 字段为:公司名称 公司代码 收入 支出 内部编号
公司資料0
------公司001
----------公司00100
---------------公司0010000
---------------公司0010001
------公司002
----------公司00200
---------------公司0020000
---------------公司0020001
----------------------公司002000100
----------公司00201 公司之间的关系是通过代码的层次性体现 每设定一层子公司 就在代码后加两个零 然后依次排序 且建立的方式 是通过先设定每个公司的绝对编号 然后再设定每个公司的母公司的绝对编号 同时设定其在该母公司下的顺序编号 还请Mickey 和 各位大大 帮忙看看 在下感激不尽
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
我沒有環境, 所以都未經測試 :
create proc zonghui @time int as begin declare @root varchar(20) declare code cursor for select 代码 from 公司表 where datalength(代码)=1 delete from 总汇表 where 时间=@time insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表 open code fech code into @root while (@@fetch_status=0) begin exec zonghui_rcf @time,@root fech code into @root end close code deallocate code end go create proc zonghui_rcf @time int, @parent varchar(20) as begin declare @code varchar(20), @benefit1 float, @benefit2 float declare code cursor for select 代码 from 公司表 where 代码<>@parent and 代码 like @parent '%' open code fech code into @code while (@@fetch_status=0) begin exec zonghui_rcf @time,@code fech code into @code end close code deallocate code select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent select @benefit2=sum(利润) from 总汇表 where 时间=@time and 代码<>@parent and 代码 like @parent '%' update 总汇表 set 利润=benefit1 benefit2 where 时间=@time and 代码=@parent end go |
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
按照Mickey大大告之的存储过程代码执行后 产生了两个存储过程zonghui和zonghui_rcf 执行存储过程后 《总汇表》里对应的时间和相应字段值皆为空 我对应的执行代码如下
var index:integer; inputstring:string; begin //取得当前时间的值 setpara(); index:=strtoint(para[2]); //提示输入要显示的时间 inputstring:= InputBox('请选择需要统计的时间', '时间', inttostr(index-1)); index:=strtoint(inputstring); //计算总汇表 adocommand1.CommandText:='exec zonghui''' inputstring ''''; adocommand1.Execute;即按以上代码执行后 结果全部为空 我再检查对应时间 《公司表》中 的字段:公司代码 收入 支出 内部编号 有值 非空 另 我在 proc zonghui里 insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表 后面 加上了 order by 内部编号 即 insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表 order by 内部编号 在 proc zonghui_rcf里 select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent 后 我加上了 and 时间=@time 即 select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent and 时间=@time 目前自己水平较菜 但自己一定会努力 自从来到Ktop后 感觉成长了许多 这里的学术气氛很好 I LIKE IT 有的时候自己感觉前面是明显的障碍和困难 但自己总是偏执的相信 一定可以克服 在困难重重的处境中 坚持 再坚持下去 庆幸的是 这里有很多朋友实力超群 且有大侠风范 我觉得这很难得 我希望自己将来也能成为其中的一员 虽然现在问题仍未解决 但无论如何都很感谢Mickey及各位大大的关注和回复 对于困难重重 狭路相逢勇者胜----我信 發表人 - baby2321 於 2005/09/27 15:50:07 發表人 - baby2321 於 2005/09/27 15:52:30 |
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
Mickey 很不好意思 断断续续耽误了好些时间 纠正了一些BUG 现将测试结果公布如下:
公司表字段:时间, 公司代码,公司名称, 收入,支出,parent(母公司代码)
总汇表字段:时间, 公司代码,公司名称, 利润
CREATE proc zonghui @time int
as
begin
declare @root varchar(20)
declare code cursor for select 代码 from 公司表 where datalength(代码)=1 delete from 总汇表 where 时间=@time
insert into 总汇表(时间, 公司代码,公司名称, 利润) select @time,公司代码,公司名称,0 from 公司表 open code
fetch code into @root
while (@@fetch_status=0)
begin
exec zonghui_rcf @time,@root
fetch code into @root
end
close code
deallocate code
end
GO CREATE proc zonghui_rcf @time int, @parent varchar(20)
as
begin
declare @code varchar(20),@benefit1 money,@benefit2 money
declare code cursor for select 代码 from 公司表 where 代码<>@parent and 代码 like @parent '%'
open code
fetch code into @code
while (@@fetch_status=0)
begin
exec zonghui_rcf @time,@code
fetch code into @code
end
close code
deallocate code select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 时间=@time and 代码=@parent
select @benefit2=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 时间=@time and 代码<>@parent and 代码 like @parent '%'
--此处我将原来的'总汇表'改为'公司表'
update 总汇表 set 利润=@benefit1 @benefit2 where 时间=@time and 代码=@parent end
GO 对应程序代码如下:
procedure Tmain.N1Click(Sender: TObject);
var
index:integer;
inputstring:string;
newform:TReport;
begin
//取得当前时间
setpara();
index:=strtoint(para[2]);
//提示用户输入要显示的时间
inputstring:= InputBox('请选择所要统计的时间', '时间', inttostr(index-1));
index:=strtoint(inputstring); adocommand1.CommandText:='exec zonghui''' inputstring '''';
adocommand1.Execute; newform:=TReport.Create(application);
newform.SetPeriod(index);
newform.QuickRep1.Preview;//在QuickRep1上我放置了两个QRDBText分别连接 总汇表 的 公司 和 利润 字段
end; 结果是:QuickRep1.Preview中(两个QRDBText)只显示了 一行总汇表的数据 利润仍然显示是0
查询数据库中的总汇表 利润 这个字段 全为0 感觉是不是在计算 '利润=@benefit1 @benefit2'出现问题
好象 总汇表 就停留在
‘insert into 总汇表(时间, 公司代码,公司名称, 利润) select @time,公司代码,公司名称,0 from 公司表’ 的处理之后 我很不解
再次感谢Mickey大大赐教
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
Mickey 你好
1.我重新建立一个数据库并在其中建立了 公司表 和 总汇表 字段和原来的公司表 总汇表一样 只不过 数据内容 是我临时写上去的(不多 只加了十来条数据) 再用您写的存储过程调试 结果竟然有数据!!!
create proc zonghui @time int as begin declare @root varchar(20) declare code cursor for select 代码 from 公司表 --原来这里的where datalength(代码)=1应去掉 否则只有datalength(代码)=1的利润显示且显示值为零 delete from 总汇表 where 时间=@time insert into 总汇表(时间, 公司, 利润) select @time,代码,0 from 公司表 open code fech code into @root while (@@fetch_status=0) begin exec zonghui_rcf @time,@root fech code into @root end close code deallocate code end go create proc zonghui_rcf @time int, @parent varchar(20) as begin declare @code varchar(20), @benefit1 float, @benefit2 float declare code cursor for select 代码 from 公司表 where 代码<>@parent and 代码 like @parent '%' open code fech code into @code while (@@fetch_status=0) begin exec zonghui_rcf @time,@code fech code into @code end close code deallocate code select @benefit1=sum(isnull(收入,0)-isnull(支出,0)) from 公司表 where 代码=@parent select @benefit2=sum(利润) from 总汇表 where 时间=@time and 代码<>@parent and 代码 like @parent '%' update 总汇表 set 利润=@benefit1 @benefit2 where 时间=@time and 代码=@parent end go2.我把原来的公司表 总汇表导出到一个新建立的空数据库里 同样用您告之的存储过程调试 结果竟然还是 无 数据(应该说显示为零)!!! 相比较之下 原来的公司表 总汇表比我临时建立的公司表 总汇表 的数据内容多一些 但字段类型完全相同 我很困惑 |
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |