+ -
当前位置:首页 → 问答吧 → oracle的统计问题

oracle的统计问题

时间:2011-10-19

来源:互联网

1、select * from recharge where loddate in 
(select max(loddate) from recharge where to_char(loddate,'yyyy')<'2007' 
有4条2006年记录;
2、select * from recharge where loddate in 
(select max(loddate) from recharge where to_char(loddate,'yyyy')='2007' 
有5条2007年的记录
3、select * from recharge where loddate in 
(select max(loddate) from recharge where to_char(loddate,'yyyy')<='2007'
应该有9条记录才对,可是这条语句的统计结果和第2条一样,只统计出2007年的5条记录,没有统计出2006年的记录,为什么会这样,是我什么地方写的不对吗?

作者: yaomin65   发布时间: 2011-10-19

你看看你第3句:
条件是<='2007',这样select出来的max(loddate)还是2007啊。
当然和你第2条的结果是一样的啦(第2条的max(loddate)也是2007);
而你1条的max(loddate)是2006

作者: yixilan   发布时间: 2011-10-19

引用 1 楼 yixilan 的回复:
你看看你第3句:
条件是<='2007',这样select出来的max(loddate)还是2007啊。
当然和你第2条的结果是一样的啦(第2条的max(loddate)也是2007);
而你1条的max(loddate)是2006



看这个解释就OK了!

作者: cosio   发布时间: 2011-10-19

那我想得到第1条和第2条语句的结果(9条记录),该怎么写呢?

作者: yaomin65   发布时间: 2011-10-19

引用 3 楼 yaomin65 的回复:
那我想得到第1条和第2条语句的结果(9条记录),该怎么写呢?

最直接的解决方法是:
SQL code
select * from recharge where loddate in  
(select max(loddate) from recharge where to_char(loddate,'yyyy')<'2007'  
union
select * from recharge where loddate in  
(select max(loddate) from recharge where to_char(loddate,'yyyy')='2007'  


在此在指出楼主的一个错误,你的三条SQL达成的效果,并不是象你所理解的查询某年的记录,而是查询某年最大时间点的记录。
比如2006年有5条记录:
2006-1-1
2006-1-2
2006-1-3
2006-12-31
2006-12-31
使用你的SQL返回的是2006-12-31的两条记录,而不是5条记录。

作者: xiaobn_cn   发布时间: 2011-10-19

SQL code

select * from recharge where loddate in  
(select distinct loddate from recharge where to_char(loddate,'yyyy')<='2007'


作者: jssg_tzw   发布时间: 2011-10-19

后面忘记一个")"

作者: jssg_tzw   发布时间: 2011-10-19

楼主!首先你的思路就错了!你理解错了那几句代码!!为什么这么说呢!请看:
drop table test;
create table test
(
time date 
);

insert into test values(to_date('2006-01-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2006-02-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2006-03-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2006-04-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2007-05-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2007-06-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2007-07-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2007-08-01'
,'yyyy-mm-dd'));
insert into test values(to_date('2007-09-01'
,'yyyy-mm-dd'));


select * from test where to_char(time,'yyyy') in
(select max(to_char(time,'yyyy')) from test 
where to_char(time,'yyyy')<'2007');
第一条: 从年份中找出小余2007的最大年份,也就是2006为最大年份,
接着从条件在最大年份2006中查询信息,也就是输出4条记录。
select * from test where to_char(time,'yyyy') in
(select max(to_char(time,'yyyy')) from test 
where to_char(time,'yyyy')='2007');
第二条:和第一条相似,只是最大年份变成了2007。结果输出5条记录
select * from test where to_char(time,'yyyy') in
(select max(to_char(time,'yyyy')) from test 
where to_char(time,'yyyy')<='2007');
第三条:和第二条完全没有区别。要想输出9条记录:
select * from test where to_char(time,'yyyy') in
(select to_char(time,'yyyy') from test 
where to_char(time,'yyyy')<='2007');
去掉max函数就可以了!!楼主你自己多想想!!

作者: pyylyj   发布时间: 2011-10-19

热门下载

更多