通过sql语句支持查询出TreeGrid需要的数据结构

  之前在项目中都是通过查询出基础数据,然后再业务层组装出TreeGrid对应的数据结构。但是个人感觉如果在提供树状结构展示的同时提供excel导出的共就比较麻烦,所以自己就写了一个。

  

----树形结构orgcode不能重复,所以用orgCode+subejctstages组装
        select m.orgCode || n.subejctstages orgCode,
                orgName,
                orglevel,
                case
                  when length(m.orgCode) = 6 and n.subejctstages is not null then
                   orgCode
                  else
                   pcode
                end pcode,
                subejctstages
          from (select distinct a.code     orgCode,
                                 a.name     orgName,
                                 a.orglevel,
                                 a.pcode
                   from org a
                   left join (select a.testid, a.testcampuscode
                               from subjectregister a
                              where a.testid = :testID
                                and a.testcampuscode like :orgCode
                              group by a.testid, a.testcampuscode
                             having count(1) > 0) c
                     on a.testid = c.testid
                       --and a.code = c.testcampuscode
                    and (a.code = c.testcampuscode or
                        a.code = substr(c.testcampuscode, 0, 5) or
                        a.code = substr(c.testcampuscode, 0, 2))
                  where a.testid = :testID1
                    and a.code like :orgCode1
                    and a.enable = :enable
                    and c.testcampuscode is not null) m
          left join (
---如果要在查询结果中增加一列,并且这列的值为指定的几个值时,可以组装一个临时表
select '' subejctstages
                       from dual
                     union
                     select '1' subejctstages
                       from dual
                     union
                     select '2' subejctstages
                       from dual) n
            on length(m.orgCode) = 6

  

猜你喜欢

转载自www.cnblogs.com/hobby0524/p/11989908.html