大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Easy
创新互联建站网站建设公司一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!专注中小微企业官网定制,成都网站建设、网站建设,塑造企业网络形象打造互联网企业效应。
SELECT id, shouyi, riqi
FROM b b1
WHERE NOT EXISTS (SELECT 1 FROM b WHERE id = b1.id AND riq b1.riqi)
WITH tmp AS
(select '张一' as names, '2014年1月' as dates, '90' as sorcs from dual union
select '张四' as names, '2014年1月' as dates, '66' as sorcs from dual union
select '张一' as names, '2014年2月' as dates, '84' as sorcs from dual union
select '张二' as names, '2014年2月' as dates, '75' as sorcs from dual union
select '张四' as names, '2014年2月' as dates, '48' as sorcs from dual union
select '张三' as names, '2014年3月' as dates, '75' as sorcs from dual union
select '张四' as names, '2014年3月' as dates, '96' as sorcs from dual union
select '张一' as names, '2014年4月' as dates, '94' as sorcs from dual union
select '张四' as names, '2014年4月' as dates, '75' as sorcs from dual union
select '张三' as names, '2014年5月' as dates, '84' as sorcs from dual union
select '张四' as names, '2014年6月' as dates, '75' as sorcs from dual union
select '张二' as names, '2014年7月' as dates, '64' as sorcs from dual union
select '张一' as names, '2014年8月' as dates, '77' as sorcs from dual )
SELECT names,
SUM(CASE WHEN dates ='2014年1月' THEN sorcs END ) AS "2014年1月" ,
SUM(CASE WHEN dates ='2014年2月' THEN sorcs END ) AS "2014年2月",
SUM(CASE WHEN dates ='2014年3月' THEN sorcs END ) AS "2014年3月"
FROM tmp
GROUP BY names
你这个是要行列互换吧?
看你的意思
大概这样理解
数据库表里面有编号为1和编号为2的
男女若干个
现在要分别统计
编号为1的男有多少个
女有多少个
编号为2的
男有多少个
女有多少个;如果是这样的话,首先先按编号分组
select
*
from
student
group
by
id;
然后再这个基础上要分别统计男和女
可以用到oracle特有的decode函数
select
sum(decode(sex,'男',1,0))
男
from
student
这样就得到了男
以此类推
整个sql语句
可以这样写
select
id
编号,sum(decode(sex,'男',1,0))
男,sum(decode(sex,'女',1,0))
女
from
student
group
by
id;
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
dual
connect
by
level
=10
---结果:
1
2
3
4
5
6
7
8
9
10
oracle时间的加减看看试一下以下sql语句就会知道:
select
sysdate
-1
from
dual
----结果减一天,也就24小时
select
sysdate-(1/2)
from
dual
-----结果减去半天,也就12小时
select
sysdate-(1/24)
from
dual
-----结果减去1
小时
select
sysdate-((1/24)/12)
from
dual
----结果减去5分钟
select
sydate-(level-1)
from
dual
connect
by
level=10
---结果是10间隔1天的时间
下面是本次例子:
select
dt,
count(satisfy_degree)
as
num
from
T_DEMO
i
,
(select
sysdate
-
(level-1)
*
2
dt
from
dual
connect
by
level
=
10)
d
where
i.satisfy_degree='satisfy_1'
and
i.insert_timedt
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
dual
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
dual
connect
by
level
=
34)
d
where
i.locate_type
=
1
and
i.created_timedt
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)
select a.group,(a.date-b.date)*24*60*60 from (select group,date from table where flag=1) a
left outer join (select group,date from table a where flag=4) b on a.group=b.group
你这个不需要分组