select depttable0_.dept_id as col_0_0_,
(select count(*) from dept_table depttable1_ where depttable1_.active='1' and depttable1_.parent_dept_code=depttable0_.dept_code and depttable1_.company_code='001') as col_4_0_  
from dept_table depttable0_
表dept_table有字段:
DEPT_ID          VARCHAR2(50)
DEPT_CODE        VARCHAR2(50)
DEPT_NAME        VARCHAR2(100)
PARENT_DEPT_CODE VARCHAR2(50)
IN_EMP_CODE        VARCHAR2(50)
ACTIVE            CHAR(1)
COMPANY_CODE      CHAR(3)
EMP_CODE          VARCHAR2(50)
UPDATE_DT         CHAR(14)
不理解,求大神分析下,跪求

解决方案 »

  1.   

    select a.dept_id as col_0_0_,
    (select count(*) 
    from dept_table b 
    where   b.active='1' 
    and b.parent_dept_code=a.dept_code 
    and b.company_code='001'
    ) as col_4_0_  
    from dept_table a
    --求每个部门的符合条件active='1' and company_code='001'的下属部门的个数
      

  2.   

    假如dept_table有三条数据
    DEPT_ID  DEPT_CODE  DEPT_NAME   PARENT_DEPT_CODE  ACTIVE   COMPANY_CODE
    1           00       运营公司        0                    1          000
    2           00       先声药业        0                    1          001
    3           0001     OTC部门         00                   1          001
    是不是执行时,从表a的第一行即dept_id=1开始执行,
    此时里面的子查询就变成
    select count(*)
    from dept_table b
    where  b.active='1' 
    and b.parent_dept_code='00' and b.company_code='001'
    此时能得到第一行结果;
    然后执行dept_id=2这行,
    select count(*)
    from dept_table b
    where  b.active='1' 
    and b.parent_dept_code='00' and b.company_code='001'
    此时能得到第二行结果;
    然后执行dept_id=3这行,
    select count(*)
    from dept_table b
    where  b.active='1' 
    and b.parent_dept_code='0001' and b.company_code='001'
    此时能得到第三行结果;
    所以总共有三行结果
    请问各位大神是这样分析的吗?我不是太确定
      

  3.   

    dept_table有多少条数据,结果就有多少条数据,因为dept_table表并没有where限定用的子句
      

  4.   

    select a.dept_id as col_0_0_,(select count(*)     from dept_table b         where   b.active='1'             and b.parent_dept_code=a.dept_code             and b.company_code='001') as col_4_0_  from dept_table a正解