省份      时间        数据1         数据2
A省    2001-01-01     10             12
A省    2001-03-31     50             30
A省    2001-06-30     120            50
A省    2001-09-30     200            100
A省    2001-12-31     500            200



。查询A省各个季度的数据,用的方法是第一季度为2001-03-31的数据,第二季度为2001-06-30减去2001-03-31的数据
第三季度为2001-09-30减去2001-06-30的数据,第四季度为2001-12-31减去2001-09-30的数据
查询结果为一下所示:
省份      时间        数据1       数据2
A省       1季度       50           30
A省       2季度       70           20
A省       3季度       80           50
A省       4季度       300          100希望各路大虾帮帮小弟,感激不尽。

解决方案 »

  1.   

    SQL> select * from emp t where t.empno <=7566;
     
    EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO
    ----- ---------- --------- ----- ----------- --------- --------- ------
     7369 SMITH      CLERK      1000 1980-12-17     800.10               20
     7499 ALLEN      SALESMAN   7698 1981-2-20     1600.00    300.00     30
     7521 WARD       SALESMAN   7698 1981-2-22     1250.00    500.00     30
     7566 JONES      MANAGER    7839 1981-4-2      2975.00               20
     
    SQL> select empno,ename,sal-lag(sal,1,0)over(order by empno asc) from emp t where t.empno <=7566;
     
    EMPNO ENAME      SAL-LAG(SAL,1,0)OVER(ORDERBYEM
    ----- ---------- ------------------------------
     7369 SMITH                               800.1
     7499 ALLEN                               799.9
     7521 WARD                                 -350
     7566 JONES                                1725
     
    SQL> 
      

  2.   

    where条件里限定下不就只剩这几条了么?
      

  3.   

    比如数据还有
    省份 时间 数据1 数据2
    A省 2001-01-01 10 12
    A省 2001-02-12 10 12
    A省 2001-03-31 50 30
    A省 2001-03-12 10 12
    A省 2001-03-20 10 12
    A省 2001-05-20 10 12
    A省 2001-05-25 10 12
    A省 2001-06-10 120 50
    A省 2001-06-26 120 50
    A省 2001-06-30 120 50
    A省 2001-07-12 120 50
    A省 2001-07-18 120 50
    A省 2001-08-18 120 50
    A省 2001-09-30 200 100
    A省 2001-10-14 200 100
    A省 2001-10-21 200 100
    A省 2001-11-09 200 100
    A省 2001-11-20 200 100
    A省 2001-12-31 500 200
      

  4.   

    where to_char(时间字段,'yyyy')='2001' and to_char(时间字段,'mmdd') in ('0331','0630','0930','1231')
    把这个限定条件加上应该就可以了.
      

  5.   

    哦,那就to_char(时间字段,'yyyy') between '2001' and '2005' and to_char(时间字段,'mmdd') in ('0331','0630','0930','1231')
    把这个限定条件加上应该就可以了.
      

  6.   

    如果每年的第一个季度都是取0331那天的,over(order by )就需要再加个partition by.
    也就是over(partition by to_char(时间字段,'yyyy') order by 时间字段 asc)
      

  7.   

    SQL语句是太戳了点,,但可以用,,自己在优化下。
    select 省份,'1季度' as 时间 ,sum(数据1) as 数据1,sum(数据2) as 数据2 from test 
    where to_char(to_date(时间,'yyyy-MM-dd'),'MM')='03' and    省份='A省'
    group by 省份,'1季度' 
    union all
    select  省份,'2季度' as 时间 ,
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06'  and  省份='A省') - 
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='03'  and  省份='A省')  as 数据1,
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06' and  省份='A省' ) - 
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='03' and  省份='A省' ) as 数据2
    from test  where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06'  and    省份='A省'
    group by 省份,'2季度' 
    union all
    select 省份,'3季度' as 时间 ,
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09' and  省份='A省' ) - 
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06' and  省份='A省'  )  as 数据1,
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'  and  省份='A省') - 
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06'   and  省份='A省') as 数据2
    from test  where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'   and    省份='A省'
    group by 省份,'3季度' 
    union all
    select 省份,'4季度' as 时间 ,
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='12'  and  省份='A省') - 
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'  and  省份='A省')  as 数据1,
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='12'  and  省份='A省') - 
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'  and  省份='A省') as 数据2
    from test  where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='12'  and    省份='A省'
    group by 省份,'4季度' 
     
      

  8.   

    查询结果:省份 时间 数据1 数据2
    A省 1季度 50 30
    A省 2季度 190 70
    A省 3季度 -40 0
    A省 4季度 300 100
      

  9.   


      1  with t as (
      2   select 'A' province,date'2001-01-01' dates,10 data1, 12 data2 from dual
      3   union all
      4   select 'A',date'2001-03-31',50,30 from dual
      5   union all
      6   select 'A',date'2001-06-30',120,50 from dual
      7   union all
      8   select 'A',date'2001-09-30',200,100 from dual
      9   union all
     10   select 'A',date'2001-12-31',500,200 from dual
     11  )
     12  select province,to_char(dates,'q "Quarter"') dates,
     13  data1-lag(data1,1,0) over (partition by to_char(dates,'yyyy') order by dates) data1,
     14  data2-lag(data2,1,0) over (partition by to_char(dates,'yyyy') order by dates) data2
     15  from t
     16* where to_char(dates,'mmdd') in ('0331','0630','0930','1231')
    SQL> /P DATES          DATA1      DATA2
    - --------- ---------- ----------
    A 1 Quarter         50         30
    A 2 Quarter         70         20
    A 3 Quarter         80         50
    A 4 Quarter        300        100
      

  10.   

    LZ,不好意思,你后面才说有年份区别的,,那就用这个好了。。看下可以不。。select * from (
    select 省份,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') as 年份,'1季度' as 时间 ,sum(数据1) as 数据1,sum(数据2) as 数据2 from test 
    where to_char(to_date(时间,'yyyy-MM-dd'),'MM')='03' and    省份='A省'
    group by 省份,'1季度' ,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') 
    union all
    select  省份,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') as 年份,'2季度' as 时间 ,
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06'  and  省份='A省') - 
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='03'  and  省份='A省')  as 数据1,
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06' and  省份='A省' ) - 
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='03' and  省份='A省' ) as 数据2
    from test  where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06'  and    省份='A省'
    group by 省份,'2季度' ,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') 
    union all
    select 省份,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') as 年份,'3季度' as 时间 ,
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09' and  省份='A省' ) - 
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06' and  省份='A省'  )  as 数据1,
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'  and  省份='A省') - 
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='06'   and  省份='A省') as 数据2
    from test  where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'   and    省份='A省'
    group by 省份,'3季度' ,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') 
    union all
    select 省份,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') as 年份,'4季度' as 时间 ,
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='12'  and  省份='A省') - 
    (select sum(数据1) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'  and  省份='A省')  as 数据1,
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='12'  and  省份='A省') - 
    (select sum(数据2) from test where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='09'  and  省份='A省') as 数据2
    from test  where to_char(to_date(时间,'yyyy-MM-dd'),'MM') ='12'  and    省份='A省'
    group by 省份,'4季度' ,to_char(to_date(时间,'yyyy-MM-dd'),'yyyy') 
     
    ) temp order by 年份,时间
      

  11.   

    不知道 lz 指的“写死的数据”是哪些?
    举个例子而已,主要看下面的 select 语句的写法是否满足要求。where to_char(dates,'mmdd') in ('0331','0630','0930','1231') 用于筛选所需要的数据,也就是 lz 所说的“第一季度为 03-31,第二季度为 06-30,第三季度为 09-30 ,第四季度为 12-31”。
      

  12.   


    大虾,小弟不是很明白
      1  with t as (
      2   select 'A' province,date'2001-01-01' dates,10 data1, 12 data2 from dual
      3   union all
      4   select 'A',date'2001-03-31',50,30 from dual
      5   union all
      6   select 'A',date'2001-06-30',120,50 from dual
      7   union all
      8   select 'A',date'2001-09-30',200,100 from dual
      9   union all
     10   select 'A',date'2001-12-31',500,200 from dual
     11  )
    是什么意思,能否解释一下,可能是因为看到这里才认为是写死了的
     12  select province,to_char(dates,'q "Quarter"') dates,
     13  data1-lag(data1,1,0) over (partition by to_char(dates,'yyyy') order by dates) data1,
     14  data2-lag(data2,1,0) over (partition by to_char(dates,'yyyy') order by dates) data2
     15  from t
     16* where to_char(dates,'mmdd') in ('0331','0630','0930','1231')
    这些语句直接写在plsql里就能运行吗?大虾
      

  13.   

    tom是用with组织了些测试数据给你,你自己的sql直接把下面select里的t替换成自己的表就可以了.
    with的用法很好,能把带子查询的语句写的很利于阅读.
      

  14.   

    自己试验了一下,可是里面的都是dates,data1,data2.....不知道该替换哪个,大虾能不能指导一二啊,
      

  15.   

    省份       时间           数据1       数据2
    province  dates        data1      data2  他的字段起名跟你的不一样, 按上面的替换下再执行.
      

  16.   

    select to_char(TBD.f_Staticdaytime, 'q "Quarter"') asas,
           TBD.f_Deadpopnum - lag(TBD.f_Deadpopnum, 1, 0) over(partition by to_char(TBD.f_Staticdaytime, 'yyyy') order by TBD.f_Staticdaytime) data1
      from TB_BAS_DAYHAZARDDATA TBD
     where to_char(TBD.f_Staticdaytime, 'mmdd') in
           ('0331', '0630', '0930', '1231')
    我按照这样替换的,但是执行提示无效数字
    TB_BAS_DAYHAZARDDATA这个为我自己的表,
      

  17.   

    各位大虾,问题已经解决了,还是使用的xman_78tom的方法,谢谢各位支持,小弟分数不多,回家给大家分数啊,再次谢谢各位的帮助。
      

  18.   

    大侠们,不好意思,又有个问题想问问你们了,
    to_char(dates,'q "Quarter"')我想改成月份的,写成to_char(dates,'q "Month"'),是不是怎样写呢?运行了一下,报错了:日期格式无法识别。
    大虾能帮我解释一下这句话的意思吗?
      

  19.   

    SQL> select to_char(sysdate,'yyyymm') from dual;
     
    TO_CHAR(SYSDATE,'YYYYMM')
    -------------------------
    201009
     
    SQL> 
    去看下oracle文档的format models那段.学习下常用的格式转换.
      

  20.   

    我又写了个月份的SQL文,还是像季度,用2月28号减去1月31号的数据,以后的依次相见,SQL文如下:
    select to_char(to_date(TBD.f_Staticdaytime, 'yyyy-MM-dd'),
                           'mon') dates,
              TBD.f_Deadpopnum - lag(TBD.f_Deadpopnum, 1, 0) over(partition by to_char(to_date(TBD.f_Staticdaytime, 'yyyy-MM-dd'), 'mm') order by TBD.f_Staticdaytime) data1
             from TB_BAS_DAYHAZARDDATA TBD
             where substr(TBD.f_Staticdaytime, 6) in
                   ('01-31','02-28','02-29','03-31','04-30','05-31','06-30','07-31','08-31','09-30','10-31','11-30','12-31')
               and substr(TBD.f_Staticdaytime, 1, 4) in ('2009')但是,结果出来了,不像季度的那样,他们没有相减,只是把数据查询出来了,数据如下:
    1 1月  10
    2 2月  14
    3 3月  15
    4 4月  19
    5 5月  27
    6 6月  31
    7 7月  33
    8 8月  39
    9 9月  45
    10 10月 55
    11 11月 69
    12 12月 80
    大虾朋友们能不能略指点一二,谢谢啦