单位代码 年度 月份 科目代码 金额  
4020202 2010 1 401010101 523652
4020202 2010 2 401010101 5236
4020202 2010 3 401010101 85965
4020202 2010 4 401010101 74596
4020202 2010 5 401010101 91236
4020205 2010 1 520000012 81236
4020205 2010 2 520000012 12536
4020205 2010 3 520000012 23444
4020205 2010 4 520000012 33555
4020205 2010 5 520000012 35454
* * * * *
* * * * *
* * * * *
-----------------------
这是对一个表的查询,这个表类似于上面这样子,其中的单位代码有很多,年度都是2010,每个单位对应的科目代码有很多,也不一定都相同,每个单位对的月份也有多个,也不一定都相同,其中每个单位每个月份每个科目对应的一个金额。
现在我想求这样一个查询语句
例:查询出 (相同单位,相同科目2月份对应的金额)/(相同单位,相同科目1月份对应的金额) <0.1的时候就把这个单位对应的2月份的这个科目的这条数据显示出来,按这样的要求把所有符合条件的数据都查询出来。!

解决方案 »

  1.   

    每个单位每个月份每个科目对应的一个金额
    ---------------------------------------
    2月份金额/1月份金额 group by 单位, 科目 ?
      

  2.   


    --不知道对不对,你测试下:
    SELECT a.单位代码, a.年度, a.月份, a.科目代码, a.金额 
    FROM(
        SELECT 单位代码, 年度, 月份, 科目代码,Sum(金额) 金额 
        FROM tab 
        GROUP BY 单位代码, 年度, 月份, 科目代码
        WHERE 月份='02' ) a
        ,
        (
        SELECT 单位代码, 年度, 月份, 科目代码,Sum(金额) 金额 
        FROM tab 
        GROUP BY 单位代码, 年度, 月份, 科目代码
        WHERE 月份='01' ) b
    WHERE a.单位代码=b.单位代码 AND a.年度=b.年度 AND a.科目代码=b.科目代码
          AND (a.金额/b.金额)<0.1;
      

  3.   


    GROUP BY 单位代码, 年度, 月份, 科目代码
        WHERE 月份='02' where 条件应该在 group by 前面。with newTable as(
    select 4020202 单位代码 ,2010 年度, 1 月份,401010101 科目代码,523652 金额 from dual
    union
    select 4020202,2010,2,401010101,5236 from dual
    union
    select 4020202,2010,3,401010101, 85965 from dual
    union
    select 4020202,2010,4 ,401010101 ,74596 from dual
    union
    select 4020202,2010, 5, 401010101, 91236 from dual
    union
    select 4020205,2010 ,1 ,520000012, 81236 from dual
    union
    select 4020205,2010, 2 ,520000012, 12536 from dual
    union
    select 4020205,2010 ,3 ,520000012, 23444 from dual
    union
    select 4020205,2010, 4 ,520000012 ,33555 from dual
    union
    select 4020205,2010, 5 ,520000012, 35454 from dual
    )
    select temp.* from 
    ( select * from newTable a where 月份 = 2  ) temp
    inner join  
    (select * from newTable where 月份 = 1) temp2    
     on 
     temp.单位代码= temp2.单位代码 and 
     temp.科目代码=temp2.科目代码 and 
      temp.年度=temp2.年度 and 
      (temp.金额/temp2.金额)<0.1
     
      

  4.   


    create table MESSAGEINFO
    (
      UNITNO  VARCHAR2(20),
      YEAR    VARCHAR2(20),
      MOUTH   VARCHAR2(20),
      LESSONS VARCHAR2(20),
      SALARY  NUMBER
    );
    insert into MESSAGEINFO....省略;SELECT unitno 单位代码,YEAR 年,mouth 月份,lessons 科目代码,salary 金额
    FROM (SELECT a.salary/b.salary PERCENT,a.UNITNO,a.YEAR,a.MOUTH,a.LESSONS,a.salary 
            FROM (SELECT * FROM messageinfo t WHERE t.mouth=2) a,(SELECT * FROM messageinfo t WHERE t.mouth=1) b
           WHERE a.unitno=b.unitno AND a.lessons=b.lessons) temp
    WHERE temp.PERCENT<0.1
      

  5.   

    -----这样符合要求吗? -----
    select b.单位代码,b.年度,b.月份,b.科目代码,b.金额 
    from t a,t b
    where
    a.单位代码=b.单位代码 and
    a.年度=b.年度 and
    a.科目代码=b.科目代码 and
    a.月份 = b.月份 -1 
    and b.金额/a.金额 < 0.1;
      

  6.   


    with tmp as(
    select '4020202' as 单位代码, '2010' as 年度, 1 as 月份,'401010101' as 科目代码,523652 as 金额 from dual union all
    select '4020202' as 单位代码, '2010' as 年度, 2 as 月份,'401010101' as 科目代码,5236 as 金额 from dual union all
    select '4020202' as 单位代码, '2010' as 年度, 3 as 月份,'401010101' as 科目代码,352 as 金额 from dual union all
    select '4020202' as 单位代码, '2010' as 年度, 4 as 月份,'401010101' as 科目代码,52352 as 金额 from dual union all
    select '4020205' as 单位代码, '2010' as 年度, 1 as 月份,'520000012' as 科目代码,0 as 金额 from dual union all
    select '4020205' as 单位代码, '2010' as 年度, 2 as 月份,'520000012' as 科目代码,5352 as 金额 from dual union all
    select '4020205' as 单位代码, '2010' as 年度, 3 as 月份,'520000012' as 科目代码,5652 as 金额 from dual union all
    select '4020205' as 单位代码, '2010' as 年度, 4 as 月份,'520000012' as 科目代码,52652 as 金额 from dual
    )
    select t.单位代码,t.科目代码,t1.月份,t.月份,t1.金额/t.金额 from tmp t, 
    (select 单位代码,科目代码,月份,金额 from tmp group by 单位代码,科目代码,月份,金额) t1 
    where t1.金额/t.金额 < 0.1
    and t.月份 - 1 = t1.月份
    and t.金额 != 0
    and t.科目代码 = t1.科目代码
    and t.单位代码 = t1.单位代码不知道是楼主的意思不