一个指标表(indexs),字段:
序号(idxNum)就是那种1.1、1.1.1、1.1.1.1……
父节点(parentID)
标准分(idxScore)
自查评表(self|Check),字段:编号、指标号、应得分、实得分、得分率
create  table [dbo].[indexs](idxNum varchar (20),parentID varchar (20),idxScore decimal(5,2))
go
insert into [dbo].[indexs] values('1','0','100')
insert into [dbo].[indexs] values('1.1','1','60')
insert into [dbo].[indexs] values('1.1.1','1.1','45')
insert into [dbo].[indexs] values('1.1.2','1.1','15')
insert into [dbo].[indexs] values('1.2','1','40')
insert into [dbo].[indexs] values('1.2.1','1.2','15')
insert into [dbo].[indexs] values('1.2.2','1.2','25')
insert into [dbo].[indexs] values('2','0','156')
insert into [dbo].[indexs] values('2.1','2','99')
insert into [dbo].[indexs] values('2.2','2','57')
select * from indexs
create table [dbo].[selfcheck](selfID int identity(1,1),idxNum varchar(20),selfShould decimal(5,2),selfReal decimal(5,2) null,selfRate decimal(5,2))
go
insert into [dbo].[selfcheck] values('1',0,0,0)
insert into [dbo].[selfcheck] values('1.1',0,0,0)
insert into [dbo].[selfcheck] values('1.1.1',45,40,0)
insert into [dbo].[selfcheck] values('1.1.2',15,10,0)
insert into [dbo].[selfcheck] values('1.2',0,0,0)
insert into [dbo].[selfcheck] values('1.2.1',15,8,0)
insert into [dbo].[selfcheck] values('1.2.2',25,19,0)
insert into [dbo].[selfcheck] values('2',0,0,0)
insert into [dbo].[selfcheck] values('2.1',99,86,0)
insert into [dbo].[selfcheck] values('2.2',57,43,0)自查评表中最底层节点的标准分和实得分是知道的,我想要得到各级父节点的标准分和实得分(各自由其包含的子节点累加得到)以及各级节点的得分率(实得分/应得分),也就是自查评表里面是0的那些地方,嘿嘿。

解决方案 »

  1.   

    --下面两个例,你自己参考着修改.
    /*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid/*
    标题:查询指定节点及其所有父节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有父节点的函数
    create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
    as
    begin
      insert into @t_level select @id
      select @id = pid from tb where id = @id and pid is not null
      while @@ROWCOUNT > 0
      begin
        insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
      end
      return
    end
    go--调用函数查询002(广州市)及其所有父节点
    select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有父节点
    select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市(所影响的行数为 2 行)
    */--调用函数查询008(西乡镇)及其所有父节点
    select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市
    007  003  宝安区
    008  007  西乡镇(所影响的行数为 4 行)
    */drop table tb
    drop function f_pid
      

  2.   

    大乌龟简直太强了,可以出个语录了!!
    建议搞个乌龟联机帮助,带目录和索引的,以取代微硬公司的SQL数据库的联机帮助,呵呵
      

  3.   


    --我这也有两个,邹老大的,
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    DECLARE @Level int
    SET @Level=1
    INSERT @t_Level SELECT @ID,@Level
    WHILE @@ROWCOUNT>0
    BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.PID,@Level
    FROM tb a,@t_Level b
    WHERE a.ID=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GO
    --上面的用户定义函数可以处理一个节点有多个父节点的情况,对于标准的树形数据而言,由于每个节点仅有一个父节点,所以也可以通过下面的用户定义函数实现查找标准树形数据的父节点。
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3))
    AS
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    WHILE @@ROWCOUNT>0
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    END
    RETURN
    END