省份 时间 数据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希望各路大虾帮帮小弟,感激不尽。
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希望各路大虾帮帮小弟,感激不尽。
解决方案 »
- sqlserver 存储过程与oracle存储过程的语法区别,应该怎么变动
- ORA-01033,ORA-01147,ORA-01110问题
- 关于oracle 中varray 的问题 如何向一个varray 中添加一个对像
- 在ORACLE中给表见了UPDATE触发器,可一条记录字段没有任何改变情况下,仍触发了该触发器?
- 关于字符串转换的问题
- 我的JOB停了,奇怪啊
- ★★★简单问题,在线等待。我安装完了9i,启动Enterprise Manager Console,以独立启动方式运行。登录却说没有监听器,这是怎么回事?我
- 求助]oracle9i for linux 安装后不能登入数据库
- 求教,必须声明 ‘...’ 组件,怎么声明啊
- Oracle12 中WM_CONCAT函数无法使用
- 令人费解的ISEDIT!!!
- 查出2个表然后再做连接查询跑不动
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>
省份 时间 数据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
把这个限定条件加上应该就可以了.
把这个限定条件加上应该就可以了.
也就是over(partition by to_char(时间字段,'yyyy') order by 时间字段 asc)
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季度'
A省 1季度 50 30
A省 2季度 190 70
A省 3季度 -40 0
A省 4季度 300 100
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
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 年份,时间
举个例子而已,主要看下面的 select 语句的写法是否满足要求。where to_char(dates,'mmdd') in ('0331','0630','0930','1231') 用于筛选所需要的数据,也就是 lz 所说的“第一季度为 03-31,第二季度为 06-30,第三季度为 09-30 ,第四季度为 12-31”。
大虾,小弟不是很明白
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里就能运行吗?大虾
with的用法很好,能把带子查询的语句写的很利于阅读.
province dates data1 data2 他的字段起名跟你的不一样, 按上面的替换下再执行.
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这个为我自己的表,
to_char(dates,'q "Quarter"')我想改成月份的,写成to_char(dates,'q "Month"'),是不是怎样写呢?运行了一下,报错了:日期格式无法识别。
大虾能帮我解释一下这句话的意思吗?
TO_CHAR(SYSDATE,'YYYYMM')
-------------------------
201009
SQL>
去看下oracle文档的format models那段.学习下常用的格式转换.
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
大虾朋友们能不能略指点一二,谢谢啦