问题解决了。。麻烦大家了,要多少分都可以了。。我自己几乎没学过数据库,所以在短时间内几乎没办法解决这些问题。机构表       
CREATE               TABLE               Agencies       
(       
PKId               INT               IDENTITY(1,1)               PRIMARY               KEY,       
AName               NVARCHAR(200),               --               机构名称       
AOrganId               Int               ,               --               对应"AgencyOrgan"表的PKId               
ATopNode               INT                       --父节点       
)   --   机构组织表 
CREATE   TABLE   AgencyOrgan 

PKId   INT   IDENTITY(1,1), 
AName   NVARCHAR(200)   PRIMARY   KEY   ,   --机构组织名称 
ALevel INT   --机构组织级别 

--       人员表   
CREATE       TABLE       Householders   
(   
PKId       INT       IDENTITY(1,1)       PRIMARY       KEY,   
AgenciesId       INT       ,       --关联"Agencies"表的PKId   
HName       NVARCHAR(100)       NOT       NULL,   
HCard       NVARCHAR(50)       ,       --身份证   
HPhone       NVARCHAR(50)       ,   
HAddress       NVARCHAR(200)       )   
--       财务表       
CREATE       TABLE       Financial   
(   
PKId       INT       IDENTITY(1,1)       PRIMARY       KEY,       
FAccount       FLOAT       ,       --金额   
FDate       DATETIME,       --       发放时间   
FReceive       BIT       ,--       是否领取   
FType       INT       ,       --       财务类型,关联"Items"表   
HouseholdersId       INT       NOT       NULL       --       关联"Householders"表   
)   
--       财务类型表   
CREATE       TABLE       Items   
(   
PKId       INT       IDENTITY(1,1)       PRIMARY       KEY,   
IName       NVARCHAR(100)       --       类型名称   
)   
测试数据: 
AgencyOrgan(如这里的最低基本也就是系级,也就是Householders   表中的人员的AgenciesId   只能是属 下最低级别的PKId) PKId           AName             ALevel 
1                   教育单位级           1 
2                   大学级                   2 
3                   学院级                   3 
4                   系级                       4   Agencies     PKId       AName           AOrganId             ATopNode   
1               院校                   1                           0 
2               四川大学           2                           1 
3               华西大学           2                           1 
4               川大计科院       3                           2 
5               川大法学院       3                           2 
6               华大计科院       3                           3 
7               川大计科系       4                           4 
8               川大法学系       4                           5 
9               华大计科系       4                           6 Householders   PKId             AgenciesId           HName             HCard             HPhone           HAddress 
1                       7                           张明                 122211             0000                 地址A   
2                       8                           李达                   222222             0000               地址B   
3                       7                           杨峰                   1111                 0000               地址C 
4                       9                           罗明                   22222               0000               地址D 
Items   PKId     IName       
1             学杂费 
2             书本费 
3             服装费 Financial   PKId             FAccount         FDate           FReceive       FType         HouseholdersId       
  1                     200               2007-1-1             0                   1                   1 
  2                     100               2007-1-8             1                   2                   2 
  3                     200               2007-8-4             1                   3                   3 
  4                     300               2007-10-2           1                   2                   4 
            [注:]这里的Items 表是动态的,随时在变化 Agencies表的级别层次也是不限的现在要统计的效果:(也就是要按动态的生成财务类型)所以假如我现在给定一个Agencies表中的机构PKID=1(即:这是要统计“院校“直属机构的财务) 
那么就应该是: 
现在给定PKId       为1,时间是2007-1-1到2007-12-31   机构名称                       学杂费            书本费         服装费               合计   
----------------------------------------------------------------------------------- 
四川大学                       200               100          200               500   
华西大学                        0               300          0                 300   同样假如我又给给定一个Agencies表中的机构PKID=2(即:这是要统计“四川大学”的财务) 
那么也应是: 机构名称                   学杂费            书本费         服装费                          合计   
---------------------------------------------------------------------------------------    
川大计科院                  200             0             200                     400   
川大法学院                  0              100            0                       100   

解决方案 »

  1.   

    你的问题 上次roy8不是给你写的很详细的吗
      

  2.   

    大部分解决了
    首先做一个函数
    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    goALTER function [dbo].[f_getdown](@ID int)
    returns varchar(100)
    as
    begin
       declare @want varchar(1000),@nowid int,@cmp int,@id_i int
       set @want=cast(@ID as varchar(10))
       declare cor cursor for
               select PKId from Agencies
       open cor
       fetch next from cor into @id_i
       while @@fetch_status=0
       begin
           select @cmp=ATopNode from Agencies where PKId=@id_i
           if(@cmp=@ID)
              begin
              set @want=@want+','+cast(@id_i as varchar(10))
              end
           while(@cmp<>0)
            begin
                set @nowid=@cmp
                select @cmp=ATopNode from Agencies where PKId=@nowid
                if @cmp=@ID
                   begin
                   set @want=@want+','+cast(@id_i as varchar(10))
                   end
            end
            fetch next from cor into @id_i
       end
       close cor
       deallocate cor
     return @want
    end
      

  3.   

    接着做了一个存储过程
    alter proc ups_tied(@PKID int)
    as
    begin
    SELECT Agencies.AName,EEE.学杂费, EEE.学本费,EEE.服装费 from 
    (select DDDD.PKId,max(case when DDDD.FType=1 then DDDD.FAccount else 0 end) as 学杂费,
           max(case when DDDD.FType=2 then DDDD.FAccount else 0 end) as 学本费,
           max(case when DDDD.FType=3 then DDDD.FAccount else 0 end) as 服装费
    from 
    (
    select  Financial.FAccount,Financial.FDate,Financial.FType,CCC.PKId from 
    (select Householders.PKId AS id,Householders.AgenciesId,BBB.PKId from Householders 
    inner join 
    (select dbo.f_getdown(AAA.PKId) AS 串,AAA.PKId from (select PKId from Agencies where ATopNode=@PKID) AAA) BBB
    ON charindex(cast(Householders.AgenciesId as varchar(10)),BBB.串)>0) CCC
    inner join 
    Financial
    on CCC.ID=Financial.HouseholdersId
    )
    DDDD
    group by DDDD.PKId) EEE
    inner join 
    Agencies 
    on Agencies.PKId=EEE.PKId
    end
      

  4.   

    没有动态生成SQL语句
    的确很麻烦
      

  5.   

    楼主的代码,实在看不了呀.
    难道你发贴时不会用code=sql处理一下吗?
      

  6.   

    CREATE       TABLE       Agencies   
    (   
    PKId       INT       PRIMARY       KEY,   
    AName       NVARCHAR(200),       --       机构名称   
    AOrganId       Int       ,       --       对应"AgencyOrgan"表的PKId   
    ATopNode       INT           --父节点   )   --       人员表   
    CREATE       TABLE       Householders   
    (   
    PKId       INT      PRIMARY       KEY,   
    AgenciesId       INT       ,       --关联"Agencies"表的PKId   
    HName       NVARCHAR(100)       NOT       NULL,   
    HCard       NVARCHAR(50)       ,       --身份证   
    HPhone       NVARCHAR(50)       ,   
    HAddress       NVARCHAR(200)       )   
    --       财务类型表   
    CREATE       TABLE       Items   
    (   
    PKId       INT     PRIMARY       KEY,   
    IName       NVARCHAR(100)       --       类型名称   
    )   
    --       财务表       
    CREATE       TABLE       Financial   
    (   
    PKId       INT      PRIMARY       KEY,       
    FAccount       FLOAT       ,       --金额   
    FDate       DATETIME,       --       发放时间   
    FReceive       BIT       ,--       是否领取   
    FType       INT       ,       --       财务类型,关联"Items"表   
    HouseholdersId       INT       NOT       NULL       --       关联"Householders"表   
    )   set nocount on
    insert Items select 1             ,'学杂费' 
    insert Items select 2             ,'书本费' 
    insert Items select 3             ,'服装费' 
    insert Financial select 1 , 200,'2007-1-1',0,                   1   ,                1 
    insert Financial select 2 , 100,'2007-1-8',1 ,                  2    ,               2 
    insert Financial select 3 , 200,'2007-8-4',1  ,                 3     ,              3 
    insert Financial select 4 , 300,'2007-10-2',1        ,           2       ,            4 
    --insert Financial select 5 , 300,'2007-10-2',1        ,           2       ,            5 insert Agencies select 1,'院校',                   1   ,            0 
    insert Agencies select 2,'四川大学',           2   ,                        1 
    insert Agencies select 3,'华西大学',           2    ,                       1 
    insert Agencies select 4,'川大计科院',       3       ,                    2 
    insert Agencies select 5,'川大法学院',       3        ,                   2 
    insert Agencies select 6,'华大计科院',       3         ,                  3 
    insert Agencies select 7,'川大计科系',       4          ,                 4 
    insert Agencies select 8,'川大法学系',       4          ,                 5 
    insert Agencies select 9,'华大计科系',       4            ,               6 insert Householders select 1,7,'张明','122211','0000 ','地址A'   
    insert Householders select 2,8,'李达','222222','0000','地址B'   
    insert Householders select 3,7,'杨峰','1111','0000','地址C' 
    insert Householders select 4,9,'罗明','22222','0000','地址D' 
    --insert Householders select 5,4,'罗明','22222','0000','地址D' 
    go
    create function f_getParent(@ID int,@atopname int)
    returns varchar(40)
    as
    begin
        declare @ret varchar(40)    while exists(select 1 from Agencies where PKId=@ID and ATopNode>@atopname)
        begin
            select @ID=b.PKId,@ret=','+rtrim(isnull(b.PKId,0))
            from
                Agencies a,Agencies b
            where
                a.PKId=@ID and b.PKId=a.ATopNode
        end
        
        set @ret=stuff(@ret,1,1,'')
        return @ret
    end
    go
    create proc proc_test
    @ATopNode int
    as
    begin
    declare @s varchar(8000)
    set @s = ''
    select @s = @s + ',['+IName+']= max(case when FType = '+ ltrim(PKId) + ' then FAccount else 0 end) '
    from items
    --print @s
    set @s =  ('select 机构名称 = cast(a.AName as varchar)'+@s+'
    ,sum(FAccount) as  合计 from (select * from Agencies where ATopNode = '+ltrim(@ATopNode )+') a
    left join Householders b on a.PKId = dbo.f_getParent(b.AgenciesId,'+ltrim(@ATopNode)+')
    left join Financial    c on b.Pkid = c.HouseholdersId group by a.AName')exec( @s)
    end go
    --- TEST
    declare @ATopNode int
    set @ATopNode = 3exec proc_test @ATopNode
    /*
    机构名称                           学杂费                                                   书本费                                                   服装费                                                   合计                                                    
    ------------------------------ ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- 
    华大计科院                          0.0                                                   300.0                                                 0.0                                                   300.0*/
    set @ATopNode = 2exec proc_test @ATopNode
    /*机构名称                           学杂费                                                   书本费                                                   服装费                                                   合计                                                    
    ------------------------------ ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- 
    川大法学院                          0.0                                                   100.0                                                 0.0                                                   100.0
    川大计科院                          200.0                                                 0.0                                                   200.0                                                 400.0*/set @ATopNode = 1exec proc_test @ATopNode/*
    机构名称                           学杂费                                                   书本费                                                   服装费                                                   合计                                                    
    ------------------------------ ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- 
    华西大学                           0.0                                                   300.0                                                 0.0                                                   300.0
    四川大学                           200.0                                                 100.0                                                 200.0                                                 500.0*/
    drop table Agencies,Items,Financial,Householdersdrop function f_getparent
    drop proc proc_test