当前位置:首页 » 编程语言 » oracle按天统计sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

oracle按天统计sql

发布时间: 2022-10-25 08:49:14

㈠ 求oracle按天查询sql语句

selectto_char(date,'yyyy-mm-dd')date,count(1)fromtablewheredate>to_date('2015-08-01','yyyy-mm-dd')anddate<to_date('2015-08-31','yyyy-mm-dd')groupbydateorderbydateasc
没有数据显示0这个不太好办,临时还没想到

㈡ Oracle数据库按时间进行分组统计数据的方法

Oracle按不同时间分组统计的sql
如下表table1:
日期(exportDate)
数量(amount)
--------------
-----------
14-2月
-08
20
10-3月
-08
2
14-4月
-08
6
14-6月
-08
75
24-10月-09
23
14-11月-09
45
04-8月
-10
5
04-9月
-10
44
04-10月-10
88
注意:为了显示更直观,如下查询已皆按相应分组排序
1.按年份分组
select
to_char(exportDate,'yyyy'),sum(amount)
from
table1
group
by
to_char(exportDate,'yyyy');
年份
数量
-----------------------------
2009
68
2010
137
2008
103
2.按月份分组
select
to_char(exportDate,'yyyy-mm'),sum(amount)
from
table1
group
by
to_char(exportDate,'yyyy-mm')
order
by
to_char(exportDate,'yyyy-mm');
月份
数量
-----------------------------
2008-02
20
2008-03
2
2008-04
6
2008-06
75
2009-10
23
2009-11
45
2010-08
5
2010-09
44
2010-10
88
3.按季度分组
select
to_char(exportDate,'yyyy-Q'),sum(amount)
from
table1
group
by
to_char(exportDate,'yyyy-Q')
order
by
to_char(exportDate,'yyyy-Q');
季度
数量
------------------------------
2008-1
22
2008-2
81
2009-4
68
2010-3
49
2010-4
88
4.按周分组
select
to_char(exportDate,'yyyy-IW'),sum(amount)
from
table1
group
by
to_char(exportDate,'yyyy-IW')
order
by
to_char(exportDate,'yyyy-IW');

数量
------------------------------
2008-07
20
2008-11
2
2008-16
6
2008-24
75
2009-43
23
2009-46
45
2010-31
5
2010-35
44
2010-40
88
PS:Oracle按时间段分组统计
想要按时间段分组查询,首先要了解level,connect
by,oracle时间的加减.
关于level这里不多说,我只写出一个查询语句:
----level
是一个伪例
select
level
from
al
connect
by
level
<=10
---结果:
1
2
3
4
5
6
7
8
9
10
oracle时间的加减看看试一下以下sql语句就会知道:
select
sysdate
-1
from
al
----结果减一天,也就24小时
select
sysdate-(1/2)
from
al
-----结果减去半天,也就12小时
select
sysdate-(1/24)
from
al
-----结果减去1
小时
select
sysdate-((1/24)/12)
from
al
----结果减去5分钟
select
sydate-(level-1)
from
al
connect
by
level<=10
---结果是10间隔1天的时间
下面是本次例子:
select
dt,
count(satisfy_degree)
as
num
from
T_DEMO
i
,
(select
sysdate
-
(level-1)
*
2
dt
from
al
connect
by
level
<=
10)
d
where
i.satisfy_degree='satisfy_1'
and
i.insert_time<dt
and
i.insert_time>
d.dt-2
group
by
d.dt

例子中的sysdate
-
(level-1)
*
2得到的是一个间隔是2天的时间
group
by
d.dt
也就是两天的时间间隔分组查询
自己实现例子:
create
table
A_HY_LOCATE1
(
MOBILE_NO
VARCHAR2(32),
LOCATE_TYPE
NUMBER(4),
AREA_NO
VARCHAR2(32),
CREATED_TIME
DATE,
AREA_NAME
VARCHAR2(512),
);
select
(sysdate-13)-(level-1)/4
from
al
connect
by
level<=34
--从第一条时间记录开始(sysdate-13)为表中的最早的日期,“34”出现的分组数(一天按每六个小时分组
就应该为4)
一下是按照每6个小时分组
select
mobile_no,area_name,max(created_time
),dt,
count(*)
as
num
from
a_hy_locate1
i
,
(select
(sysdate-13)-(level-1)/4
dt
from
al
connect
by
level
<=
34)
d
where
i.locate_type
=
1
and
i.created_time<dt
and
i.created_time>
d.dt-1/4
group
by
mobile_no,area_name,d.dt

另外一个方法:
--按六小时分组
select
trunc(to_number(to_char(created_time,
'hh24'))
/
6),count(*)
from
t_test
where
created_time
>
trunc(sysdate
-
40)
group
by
trunc(to_number(to_char(created_time,
'hh24'))
/
6)
--按12小时分组
select
trunc(to_number(to_char(created_time,
'hh24'))
/
6),count(*)
from
t_test
where
created_time
>
trunc(sysdate
-
40)
group
by
trunc(to_number(to_char(created_time,
'hh24'))
/
6)

㈢ oracle 查询一段时间内每一天的统计数据sql怎么写

还有这么到的???2015-09-01到2014-09-12

select date,count(*) as number from table_name where date = '2015-09-01'
union
select date,count(*) as number from table_name where date = '2015-09-02'

如果天数比较少 可以这么干,如果比较多可以写存储过程,循环搞

㈣ oracle数据库 date时间类型查询昨天、前7天、前30天、前1年的数据,sql语句怎么写

1、打开plsql,连接上oracle数据库,使用sql语句创建一张测试表。

㈤ oracle统计查询 sql语句应该怎么写

select substrb(create_time,1,4) "年份",
sum(decode(substrb(create_time,6,2),'01',commission,0)) "1月",
sum(decode(substrb(create_time,6,2),'02',commission,0)) "2月",
sum(decode(substrb(create_time,6,2),'03',commission,0)) "3月",
sum(decode(substrb(create_time,6,2),'04',commission,0)) "4月",
sum(decode(substrb(create_time,6,2),'05',commission,0)) "5月",
sum(decode(substrb(create_time,6,2),'06',commission,0)) "6月",
sum(decode(substrb(create_time,6,2),'07',commission,0)) "7月",
sum(decode(substrb(create_time,6,2),'08',commission,0)) "8月",
sum(decode(substrb(create_time,6,2),'09',commission,0)) "9月",
sum(decode(substrb(create_time,6,2),'10',commission,0)) "10月",
sum(decode(substrb(create_time,6,2),'11',commission,0)) "11月",
sum(decode(substrb(create_time,6,2),'12',commission,0)) "12月"
from test
group by substrb(create_time,1,4)
此语句是按create_time字段是字符型给出的,如果你的表中此字段是日期型,则进行一下转化

㈥ oracle统计查询 sql语句应该怎么写

select
substrb(create_time,1,4)
"年份",
sum(decode(substrb(create_time,6,2),'01',commission,0))
"1月",
sum(decode(substrb(create_time,6,2),'02',commission,0))
"2月",
sum(decode(substrb(create_time,6,2),'03',commission,0))
"3月",
sum(decode(substrb(create_time,6,2),'04',commission,0))
"4月",
sum(decode(substrb(create_time,6,2),'05',commission,0))
"5月",
sum(decode(substrb(create_time,6,2),'06',commission,0))
"6月",
sum(decode(substrb(create_time,6,2),'07',commission,0))
"7月",
sum(decode(substrb(create_time,6,2),'08',commission,0))
"8月",
sum(decode(substrb(create_time,6,2),'09',commission,0))
"9月",
sum(decode(substrb(create_time,6,2),'10',commission,0))
"10月",
sum(decode(substrb(create_time,6,2),'11',commission,0))
"11月",
sum(decode(substrb(create_time,6,2),'12',commission,0))
"12月"
from
test
group
by
substrb(create_time,1,4)
此语句是按create_time字段是字符型给出的,如果你的表中此字段是日期型,则进行一下转化

㈦ oracle 用SQL查询一个时间段每天的数据量

按日做汇总啊:


selecttrunc(datecol)日期,sum(数量)数量和,count(1)数据量
fromtablex
whereto_char(datecol,'yyyymm')='201305'
groupbytrunc(datecol);

㈧ oracle数据库中如何实现数据的跨天分时段统计

select
to_char(日期,'yyyy-mm-dd'),
count(1)
from
tab
where
to_char(日期,'hh24miss') between '200000' and '235959'
or
to_char(日期,'hh24miss') between '000000' and '080000'
group by to_char(日期,'yyyy-mm-dd');

㈨ oracle 查询一段时间内每一天的统计数据sql怎么写

可以写一个简单的procere来实现,原理是遍历日期范围,并查询日期资料笔数,写入表。
数据源表test03
1 2016-06-01 1
2 2016-06-02 1
3 2016-06-05 1
4 2016-06-04 1
5 2016-06-04 1

procere代码如下:
create or replace procere loop_by_date(pbeg_tim in varchar2,--开始日期
pend_tim in varchar2,--结束日期
errmessage out varchar2) is

nCount number(10); --总天数
i_point number(10); --当天
is_zero number(10); --当天是否有记录
begin
nCount := 0;
i_point := 0;
is_zero := 0;

select ROUND(to_date(pend_tim, 'yyyy-mm-dd') -
to_date(pbeg_tim, 'yyyy-mm-dd'))
into nCount
from al;

delete from test02;

<<fst_loop>>
loop

select count(*)
into is_zero
from test03
where date1 =
to_char(to_date(pbeg_tim, 'yyyy-mm-dd') + i_point, 'yyyy-mm-dd');

insert into test02
(date01, nccount)
values
(to_char(to_date(pbeg_tim, 'yyyy-mm-dd') + i_point, 'yyyy-mm-dd'),
is_zero);

i_point := i_point + 1;
exit fst_loop when i_point >= nCount;
end loop fst_loop;
--end;

end loop_by_date;

传入参数"2016-06-01"~~"2016-06-10"并执行,结果写入test02为:
1 2016-06-01 1
2 2016-06-02 1
3 2016-06-03 0
4 2016-06-04 2
5 2016-06-05 1
6 2016-06-06 0
7 2016-06-07 0
8 2016-06-08 0
9 2016-06-09 0

㈩ oracle日结统计,求大神帮忙分析一下统计SQL如何书写

表应该加上注释,要不没法写。
大概思路是这样的,首先统计出每一天每一个员工最晚一次的日结时间。这里分为两种情况,周5和其他日期需要分别判断。
然后将用户表和刚才的日结汇总表进行左连接,日结汇总表为空的部分就意味着该员工没有日结,外面套一层,count一下即可。