模拟:
先建一个表:
CREATE TABLE temp(id number,hiredate date,sal number);
往里面插一些数据(包括2月份最后一天的数据,3,4,5月份每天的数据,6月份前4天的数据)。
插入后的结果如下所示:
SQL> select * from temp;
ID HIREDATE SAL
---------- -------------- ----------
0 28-2月 -13 0
1 01-3月 -13 1
2 02-3月 -13 2
3 03-3月 -13 3
4 04-3月 -13 4
5 05-3月 -13 5
6 06-3月 -13 6
7 07-3月 -13 7
8 08-3月 -13 8
9 09-3月 -13 9
10 10-3月 -13 10
ID HIREDATE SAL
---------- -------------- ----------
11 11-3月 -13 11
12 12-3月 -13 12
13 13-3月 -13 13
14 14-3月 -13 14
15 15-3月 -13 15
16 16-3月 -13 16
17 17-3月 -13 17
18 18-3月 -13 18
19 19-3月 -13 19
20 20-3月 -13 20
21 21-3月 -13 21
ID HIREDATE SAL
---------- -------------- ----------
22 22-3月 -13 22
23 23-3月 -13 23
24 24-3月 -13 24
25 25-3月 -13 25
26 26-3月 -13 26
27 27-3月 -13 27
28 28-3月 -13 28
29 29-3月 -13 29
30 30-3月 -13 30
31 31-3月 -13 31
32 01-4月 -13 32
ID HIREDATE SAL
---------- -------------- ----------
33 02-4月 -13 33
34 03-4月 -13 34
35 04-4月 -13 35
36 05-4月 -13 36
37 06-4月 -13 37
38 07-4月 -13 38
39 08-4月 -13 39
40 09-4月 -13 40
41 10-4月 -13 41
42 11-4月 -13 42
43 12-4月 -13 43
ID HIREDATE SAL
---------- -------------- ----------
44 13-4月 -13 44
45 14-4月 -13 45
46 15-4月 -13 46
47 16-4月 -13 47
48 17-4月 -13 48
49 18-4月 -13 49
50 19-4月 -13 50
51 20-4月 -13 51
52 21-4月 -13 52
53 22-4月 -13 53
54 23-4月 -13 54
ID HIREDATE SAL
---------- -------------- ----------
55 24-4月 -13 55
56 25-4月 -13 56
57 26-4月 -13 57
58 27-4月 -13 58
59 28-4月 -13 59
60 29-4月 -13 60
61 30-4月 -13 61
63 01-5月 -13 63
64 02-5月 -13 64
......
92 31-5月 -13 92
94 1-6月 -13 94
95 2-6月 -13 95
96 3-6月 -13 96
97 4-6月 -13 97
注意:这里没有ID为62,93的数据。
统计出每个月的sal总和.
select sum(sal) from temp group by trunc(hiredate,'month') order by trunc(hiredate,'month')
;
统计出每个月的sal个数即记录数。
select count(sal) from temp group by trunc(hiredate,'month') order by trunc
(hiredate,'month') ;
统计出十天的sal记录数。
select count(sal) from temp where hiredate between to_date('2013-3-3 13:53:22','yyyy-mm-dd
hh24:mi:ss') and to_date('2013-3-13 13:53:22','yyyy-mm-dd hh24:mi:ss');
统计出某天所在月的第一天至第十天的记录(上旬)
select count(sal) from temp where hiredate between trunc(to_date('2013-3-3 13:53:22','yyyy-
mm-dd hh24:mi:ss'),'month') and trunc(to_date('2013-3-3 13:53:22','yyyy-mm-dd
hh24:mi:ss'),'month')+9;
统计出某天所在月的第十一天至第二十天的记录(中旬)
select count(sal) from temp where hiredate between trunc(to_date('2013-3-3 13:53:22','yyyy
-mm-dd hh24:mi:ss'),'month')+10 and trunc(to_date('2013-3-3 13:53:22','yyyy-mm-dd
hh24:mi:ss'),'month')+19;
统计出某天所在月的第二十一天至最后一天的记录(下旬)
select count(sal) from temp where hiredate between trunc(to_date('2013-3-3 13:53:22','yyyy-
mm-dd hh24:mi:ss'),'month')+20 and last_day(to_date('2013-3-3 13:53:22','yyyy-mm-dd
hh24:mi:ss'))
想输出每月上中下旬sal的记录数:
DECLARE
v_shangxun_count number;
v_zhongxun_count number;
v_xiaxun_count number;
v_start_date date;
v_end_date date;
BEGIN
v_start_date:=to_date('2013-2-28 13:53:22','yyyy-mm-dd hh24:mi:ss');
v_end_date:=to_date('2013-6-4 13:53:22','yyyy-mm-dd hh24:mi:ss');
FOR i IN 1..months_between(trunc(v_end_date,'month'),trunc(v_start_date,'month'))+1 LOOP
select count(sal) into v_shangxun_count from temp where hiredate between trunc
(v_start_date,'month') and trunc(v_start_date,'month')+10;
select count(sal) into v_zhongxun_count from temp where hiredate between trunc
(v_start_date,'month')+10 and trunc(v_start_date,'month')+20;
select count(sal) into v_xiaxun_count from temp where hiredate between trunc
(v_start_date,'month')+20 and last_day(v_start_date);
dbms_output.put_line(v_start_date||' shangxun:'||v_shangxun_count);
dbms_output.put_line(v_start_date||' zhongxun:'||v_zhongxun_count);
dbms_output.put_line(v_start_date||' xiaxun:'||v_xiaxun_count);
/* dbms_output.put_line(i);
*/ v_start_date:=add_months(v_start_date,1);
/* dbms_output.put_line(months_between(trunc(v_end_date,'month'),trunc
(v_start_date,'month')));
*/ END LOOP;
END;
输出结果:
28-2月 -13 shangxun:0
28-2月 -13 zhongxun:0
28-2月 -13 xiaxun:1
31-3月 -13 shangxun:10
31-3月 -13 zhongxun:10
31-3月 -13 xiaxun:11
30-4月 -13 shangxun:10
30-4月 -13 zhongxun:10
30-4月 -13 xiaxun:10
31-5月 -13 shangxun:10
31-5月 -13 zhongxun:10
31-5月 -13 xiaxun:11
30-6月 -13 shangxun:4
30-6月 -13 zhongxun:0
30-6月 -13 xiaxun:0