oracle的统计问题
时间:2011-10-19
来源:互联网
(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
条件是<='2007',这样select出来的max(loddate)还是2007啊。
当然和你第2条的结果是一样的啦(第2条的max(loddate)也是2007);
而你1条的max(loddate)是2006
作者: yixilan 发布时间: 2011-10-19
你看看你第3句:
条件是<='2007',这样select出来的max(loddate)还是2007啊。
当然和你第2条的结果是一样的啦(第2条的max(loddate)也是2007);
而你1条的max(loddate)是2006
看这个解释就OK了!
作者: cosio 发布时间: 2011-10-19
作者: yaomin65 发布时间: 2011-10-19
那我想得到第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
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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28