起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2194|回复: 8

[结贴] 树形递归求和(查询)!

  [复制链接]

29

主题

118

帖子

282

积分

中级会员

Rank: 3Rank: 3

积分
282
QQ
发表于 2013-7-17 15:02:46 | 显示全部楼层 |阅读模式
1、需求:在treeGrid,目前只存储了末级数据,而客户需要能自动逐级求和到根节点。

2、实现:我目前希望通过扩充query动作实现。请问如何实现?

3、我的概念及关系如下:
fIDPidCodeNameSNumNTNL
C93D3B5381254D13A43E82E7E40E8E4E01010
EC83EB6C9AEC4AEE8C1F0D0C19868634C93D3B5381254D13A43E82E7E40E8E4E010101011
701299C8475F443E849E37A365D61F57EC83EB6C9AEC4AEE8C1F0D0C198686340101010101011nkLeaf2
69519A6398D9493CA97AC14EA0E5026FC93D3B5381254D13A43E82E7E40E8E4E010201021
615E8F1F0FD54DFAA6F3118610E8C2BD69519A6398D9493CA97AC14EA0E5026F0102010102016nkLeaf2
2473CA8DC482495EB7F7910ACBD897B702025nkLeaf0

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
36149
发表于 2013-7-17 18:00:11 | 显示全部楼层
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

29

主题

118

帖子

282

积分

中级会员

Rank: 3Rank: 3

积分
282
QQ
 楼主| 发表于 2013-8-23 13:52:04 | 显示全部楼层
这种方法解决不了需求呀。
回复 支持 反对

使用道具 举报

37

主题

713

帖子

3310

积分

内部用户

积分
3310
发表于 2013-8-23 13:59:02 | 显示全部楼层
本帖最后由 jholy 于 2013-8-23 14:00 编辑

两个方案
   1、数据库运算:数据库里构造一个视图,在视图里算出各级的合计
   2、代码运算:SQL查出数据,增加一个自定义合计列,用代码遍历数据集,合计出各级的合计


回复 支持 反对

使用道具 举报

29

主题

118

帖子

282

积分

中级会员

Rank: 3Rank: 3

积分
282
QQ
 楼主| 发表于 2013-8-23 14:10:46 | 显示全部楼层
希望第二种方式,但是不知道怎么动手。
回复 支持 反对

使用道具 举报

29

主题

118

帖子

282

积分

中级会员

Rank: 3Rank: 3

积分
282
QQ
 楼主| 发表于 2013-8-23 14:11:45 | 显示全部楼层
能否直接在query中扩充?
回复 支持 反对

使用道具 举报

29

主题

118

帖子

282

积分

中级会员

Rank: 3Rank: 3

积分
282
QQ
 楼主| 发表于 2013-8-26 17:32:38 | 显示全部楼层
系统提醒hehongbo已经回复,怎么进来了看不到呢?
回复 支持 反对

使用道具 举报

4

主题

87

帖子

195

积分

初级会员

Rank: 2

积分
195
QQ
发表于 2013-8-27 14:49:03 | 显示全部楼层
本帖最后由 hehongbo 于 2013-8-27 17:09 编辑

  1. //当不需要生成级别,就是数据表中已经维护了级别的情况下,就输入如下代码
  2. public static void Sum() throws Exception{
  3.        int max=getMaxLevel();
  4.        while(max>0)
  5.       {
  6.              updateNum(max);//参照等级,依次更新它们的数量
  7.              max--;
  8.        }
  9. }

  10. 需要调用的方法代码如下:
  11. public static void getMaxLevel() throws Exception{
  12.       java.sql.Connection conn = null;
  13.       java.sql.PreparedStatement pstmt = null
  14.       ResultSet rs=null;
  15.       int max=0;         
  16.        try {
  17.           // 取得数据库连接
  18.                 conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
  19.                 pstmt = conn.prepareStatement("select max(fLevel) as tLevel from PA_FZ");
  20.                 rs=pstmt.executeQuery();
  21.                 rs.next();
  22.                max=rs.getInt("tLevel");
  23.             } catch (NamingException e) {
  24.                      throw new RuntimeException(e);
  25.             } catch (SQLException e) {
  26.                     throw new RuntimeException(e);
  27.             } finally {
  28.                     rs.close();
  29.                     rs=null;
  30.                   try {
  31.                             if (pstmt != null) {
  32.                                    pstmt.close();
  33.                              }
  34.                       } catch (SQLException e) {
  35.                               throw new RuntimeException(e);
  36.                          }
  37.                 }
  38.                  return max;
  39. }

  40. //参照等级,依次更新它们的数量
  41. public static void updateNum(int init) throws Exception{
  42.       java.sql.Connection conn = null;
  43.       java.sql.PreparedStatement pstmt = null;
  44.           try {
  45.           // 取得数据库连接
  46.                 conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
  47.                 pstmt = conn.prepareStatement("update PA_FZ, (select fParent,sum(fNum) as tNum from PA_FZ where fLevel=? group by fParent) b set fNum=b.tNum where b.fParent = PA_FZ.fID ");
  48.                pstmt.setInt(1, init);
  49.                pstmt.execute();
  50.             } catch (NamingException e) {
  51.                     throw new RuntimeException(e);
  52.             } catch (SQLException e) {
  53.                     throw new RuntimeException(e);
  54.             } finally{
  55.                      try {
  56.                                if (pstmt != null) {
  57.                                       pstmt.close();
  58.                                 }
  59.                     } catch (SQLException e) {
  60.                              throw new RuntimeException(e);
  61.                     }
  62.            }
  63.                
  64. }
复制代码
远程的联系方法 QQ 2747052626。添加好友时,需要填写论坛账号
发远程时同时也发一下
回复 支持 反对

使用道具 举报

4

主题

87

帖子

195

积分

初级会员

Rank: 2

积分
195
QQ
发表于 2013-8-27 16:04:04 | 显示全部楼层
本帖最后由 hehongbo 于 2013-8-27 17:01 编辑

//数据库中级别需要自己来维护的,数据库增加级别字段且写如下代码:
public static void Sum() throws Exception{
        updateRootLevel();//设置根目录的级别
        int level=1;
        while(hasChildLevel(level)>0)//判断该级别下边是否有子节点
        {
               updateLevel(level);//逐级设置级别等级
               level++;
        }
       while(level>0)
       {
              updateNum(level);//参照等级,依次更新它们的数量
              level--;
       }
}

需要调用的方法如下:
//先获得父为空的数据,等级设置为1
public static void updateRootLevel() throws Exception{
           java.sql.Connection conn = null;
           java.sql.PreparedStatement pstmt = null;
          try {
                    conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
                    pstmt = conn.prepareStatement("update PA_FZ set fLevel=1 where fParent is null");
                    pstmt.execute();
           } catch (NamingException e) {
                     throw new RuntimeException(e);
           } catch (SQLException e) {
                     throw new RuntimeException(e);
          } finally {
                     try {
                               if (pstmt != null) {
                                               pstmt.close();
                                  }
                        } catch (SQLException e) {
                                    throw new RuntimeException(e);
                        }
                    }
}

//判断该级别下边是否有子节点
public static int hasChildLevel(int init) throws Exception{
               java.sql.Connection conn = null;
               java.sql.PreparedStatement pstmt = null;
               ResultSet rs=null;
               int count=0;
                try {
                          // 取得数据库连接
                          conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
                          pstmt = conn.prepareStatement("select count(*) as countsum from PA_FZ b where b.fParent in (select a.fID from PA_FZ a where a.fLevel=?)");
                          pstmt.setInt(1, init);
                          rs=pstmt.executeQuery();
                          rs.next();
                          count=rs.getInt("countsum");
                } catch (NamingException e) {
                             throw new RuntimeException(e);
                } catch (SQLException e) {
                             throw new RuntimeException(e);
                } finally {
                               rs.close();
                               rs=null;
                               try {
                                      if (pstmt != null) {
                                               pstmt.close();
                                      }
                                } catch (SQLException e) {
                                         throw new RuntimeException(e);
                                }
              }
                             return count;
}

//3.给子也赋值相应的等级
public static void updateLevel(int init) throws Exception{
                  java.sql.Connection conn = null;
                  java.sql.PreparedStatement pstmt = null;
                  try {
                         // 取得数据库连接
                          conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
                          pstmt = conn.prepareStatement("update PA_FZ b,PA_FZ a set b.fLevel=? where b.fParent = a.fID and a.fLevel=? ");
                          pstmt.setInt(1, init+1);
                          pstmt.setInt(2, init);
                          pstmt.execute();
                        } catch (NamingException e) {
                                      throw new RuntimeException(e);
                        } catch (SQLException e) {
                                      throw new RuntimeException(e);
                       } finally {
                                  try {
                                       if (pstmt != null) {
                                                pstmt.close();
                                        }
                                  } catch (SQLException e) {
                                               throw new RuntimeException(e);
                                  }
                            }

}

//取得数字最大的等级
public static int getMaxLevel() throws Exception{
                java.sql.Connection conn = null;
                java.sql.PreparedStatement pstmt = null;
                ResultSet rs=null;
               int max=0;
                try {
                       // 取得数据库连接
                        conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
                        pstmt = conn.prepareStatement("select max(fLevel) as tLevel from PA_FZ");
                        rs=pstmt.executeQuery();
                        rs.next();
                        max=rs.getInt("tLevel");
                    } catch (NamingException e) {
                                  throw new RuntimeException(e);
                    } catch (SQLException e) {
                                   throw new RuntimeException(e);
                   } finally {
                                  rs.close();
                                  rs=null;
                         try {
                                  if (pstmt != null) {
                                            pstmt.close();
                                     }
                         } catch (SQLException e) {
                                       throw new RuntimeException(e);
                         }
                   }
                         return max;
}

//参照等级,依次更新它们的数量
public static void updateNum(int init) throws Exception{
                java.sql.Connection conn = null;
                java.sql.PreparedStatement pstmt = null;
                try {
                           // 取得数据库连接
                            conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
                            pstmt = conn.prepareStatement("update PA_FZ, (select fParent,sum(fNum) as tNum from PA_FZ where fLevel=? group by fParent) b set fNum=b.tNum where b.fParent = PA_FZ.fID ");  
                            pstmt.setInt(1, init);
                             pstmt.execute();
                      } catch (NamingException e) {
                                    throw new RuntimeException(e);
                      } catch (SQLException e) {
                                    throw new RuntimeException(e);
                      } finally {
                               try {
                                      if (pstmt != null) {
                                                pstmt.close();
                                       }
                                 } catch (SQLException e) {
                                               throw new RuntimeException(e);
                                 }
                     }

}

远程的联系方法 QQ 2747052626。添加好友时,需要填写论坛账号
发远程时同时也发一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2025-5-10 19:17 , Processed in 0.058058 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表