开发环境:eclipse+struts1.2+hibernate
sql语句牵扯到3个表:Article,User,Dept。它们的字段分别为:
Article:id,passed,putoutPersonId(发布人id)    //passed为1的时候表示审核通过
User:id,deptId,userName
Dept:id,deptName 
表关系:
User表的主键id是Article表putoutPersonId的外键。Dept表的主键id是User表deptId的外键。
请分别用Hql和sql完成以下要求:
1.查看所有部门或某部门文章(Article)的发布总数,通过总数,未通过总数。
2.查看所有或某一发布人发布文章的总数、通过总数、未通过总数。
在此谢过大家!

解决方案 »

  1.   

    /*
    Article:id,passed,putoutPersonId(发布人id)    //passed为1的时候表示审核通过 
    User:id,deptId,userName 
    Dept:id,deptName 
    1.查看所有部门或某部门文章(Article)的发布总数,通过总数,未通过总数。 
    2.查看所有或某一发布人发布文章的总数、通过总数、未通过总数。 
    */
    1.查看所有部门或某部门文章(Article)的发布总数,通过总数,未通过总数。 
    select c.id
    count(a.id),
    count(case when passed=1 then 1 else 0 end),
    count(case when passed=0 then 1 else 0 end)
    from 
    article a,
    user b,
    dept c
    where a.putoutPersonId=b.id(+) and a.id=c.id(+)
    group by c.id
    --如果是某个部门 and c.id='???'
    2.查看所有或某一发布人发布文章的总数、通过总数、未通过总数。
    同上
      

  2.   


    1.查看所有部门或某部门文章(Article)的发布总数,通过总数,未通过总数。
    select deptname,count(*) as 发布总数,
    sum(decode(passed,1,1,0)) as 通过总数,
    sum(decode(passed,0,1,0)) as 未通过总数
    from article,dept,usern where article.putoutPersonId=user.id and user.deptid=dept.id
    group by deptname
      

  3.   

    1.查看所有部门或某部门文章(Article)的发布总数,通过总数,未通过总数。
    select b.deptname,count(*) as 发布总数,
    sum(case when a.passed=1 then 1 else 0 end) as 通过总数,
    sum(case when a.passed=0 then 1 else 0 end) as 未通过总数
    from article a,dept b,user c
    where a.putoutPersonId=c.id and c.deptid=b.id
    group by b.deptname2.查看所有或某一发布人发布文章的总数、通过总数、未通过总数。 
    select c.id,count(*) as 发布总数,
    sum(case when a.passed=1 then 1 else 0 end) as 通过总数,
    sum(case when a.passed=0 then 1 else 0 end) as 未通过总数
    from article a,dept b,user c
    where a.putoutPersonId=c.id and c.deptid=b.id
    group by c.id