您的位置:程序门 -> db2 -> 基础类



高手请进,我有一个sql写不出来。请大家指点----------------------------------急--------------满分送出-----------------在线等待!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


高手请进,我有一个sql写不出来。请大家指点----------------------------------急--------------满分送出-----------------在线等待!
发表于:2007-10-24 12:28:20 楼主
一共有两个表

一个是机构表分别如下:
      unit
      机构编号     机构名称         上级机构编号
      unitid       unitname       parentunit_id
      1                 成都                 0
      2                 青羊区             1


还有一个发放历史表如下:
      jzff_history
      机构编号       发放家庭编号         发放人数           发放资金           发放时间
      unitid       jzfffamilyid     jzfffsum       jzffmoney       jzfftime
          1                 1                           3                 300.00           2007-5-2
          1                 2                           2                 250.00           2007-5-2
          1                 1                           3                 300.00           2007-6-2

请想我想查出成都市青羊区2007年5月到2007年6月,一共放发了多少户,多少人数
       
           
           
       
     
   
发表于:2007-10-24 13:42:221楼 得分:0
有个问题
你的   jzff_history   的机构编号是什么意思?   怎么表示出青羊区的?

发表于:2007-10-24 14:19:072楼 得分:0
jzff_history   历史发放表
发表于:2007-10-24 15:37:013楼 得分:0
select   count(unitid)   as   family_count,   sum(jzffsum)   as   people_sum
from     unit   left   join  
          (   select   distinct   unitid,   jzfffamilyid,   jzfffsum   from   jzff_history   where   jzfftime   > ='2007-5-1'   and   jzfftime   <'2007-7-1')   temp
            on   unit.unitid   =   temp.unitid
where   unitname   =   '青羊区'
group   by   temp.unitid,temp.jzfffamilyid
发表于:2007-10-24 16:23:024楼 得分:0
select   count(a.unitid)   as   family_count,   sum(a.jzffsum)   as   people_sum  
from     jzff_history   a
where   a.unitid   =   2   and   a.jzfftime     > =   to_date('2007-05-01')   and   jzfftime     <   to_date('2007-07-01')
发表于:2007-10-24 16:54:595楼 得分:0
jzff_history   里家庭是有重复的,如果要去除重复记录,楼上的方法恐怕不行.
还有,不知道是统计"人数"还是统计"人次"?   如果是统计"人数"的话,那么每户的人数是不是固定的?发生变化的怎么统计呢?
楼主再说明一下吧
发表于:2007-10-29 11:58:566楼 得分:0
找到青羊区的机构代码     a     代表青羊区   b,成都市
   
select         unitid   ,   count(   jzfffamilyid)   ,sum(jzfffsum)
from   jzff_history   as     t
where   unitid   in(

select   a.unitid     ,a.   unitname   from   unit   a   ,   unit     b  
    where   a.parentunit_id=b.   unitid
    and     a.unitname   ="青羊区"
)

and   t.jzfftime       > =   to_date(   '2007-05-01   ')   and   jzfftime       <   to_date(   '2007-07-01   ')  
group   by   unitid  
发表于:2007-11-02 13:14:257楼 得分:0
简单的问题没必要那么复杂!!!

select   distinct   count(b.jzfffamilyid)   as   户数,sum(b.jzfffsum)   as   人数,   jzfftime   as   时间

from   机构表   a,jzff_history   b

where   a.unitid=2   and   b.jzfftime   between   '2007-05-01'   and   '2007-06-30'
发表于:2007-11-05 20:03:168楼 得分:0
楼上同志,要是没有那几条表数据怎么知道unit=2?   所以肯定是不对的


快速检索

最新资讯
热门点击