oracle 求sql语句 按照日期范围分组查询。请务必写出测试过的代码,有难度才有进步,谢谢大家。 5

数据库存的字段有:开始日期startdate,结束日期enddate,填写日期autualdate,指标描述desc,指标值value等字段,用户可以通过日历控件查找在某... 数据库存的字段有:开始日期startdate,结束日期enddate,填写日期autualdate,指标描述desc,指标值value等字段,用户可以通过日历控件查找在某一范围的的指标值,比如他的查找范围为2013年5月17号到5月23号,根据这个日期分周:如5月13号至19号之间为第20周,5月20号至5月26号为第21周,其中,填写日期就是第几周里面开始和结束日期的7天中其中某一天,怎样能得到下面的样式:求高手解答,不要思路,说的容易做起来难。我财富值只有这么多,全给了 希望能解答。明天要完成任务。哎
日期范围 指标描述 指标值
5月17-5月19号 描述1 指标1
5月20号-5月26号 描述2 指标2
展开
 我来答
badkano
推荐于2020-12-19 · 知道合伙人体育行家
badkano
知道合伙人体育行家
采纳数:144775 获赞数:885381
团长

向TA提问 私信TA
展开全部

你把时间先说明白了

你一下给了三个时间呢,以哪个时间的范围为准啊

---------补充---------

问了你半天也不答,按startdate的时间为准了

--建表如下

create table test
(startdate date,
enddate date,
autualdate date,
"desc" varchar2(10),
"value" int)

--插入数据

insert into test values (to_date('2013-05-17','yyyy-mm-dd'),to_date('2013-05-17','yyyy-mm-dd'),to_date('2013-05-17','yyyy-mm-dd'),'a',100);
insert into test values (to_date('2013-05-18','yyyy-mm-dd'),to_date('2013-05-18','yyyy-mm-dd'),to_date('2013-05-18','yyyy-mm-dd'),'a',200);
insert into test values (to_date('2013-05-19','yyyy-mm-dd'),to_date('2013-05-19','yyyy-mm-dd'),to_date('2013-05-19','yyyy-mm-dd'),'a',300);
insert into test values (to_date('2013-05-23','yyyy-mm-dd'),to_date('2013-05-23','yyyy-mm-dd'),to_date('2013-05-23','yyyy-mm-dd'),'a',400);
insert into test values (to_date('2013-05-17','yyyy-mm-dd'),to_date('2013-05-17','yyyy-mm-dd'),to_date('2013-05-17','yyyy-mm-dd'),'b',800);
insert into test values (to_date('2013-05-23','yyyy-mm-dd'),to_date('2013-05-23','yyyy-mm-dd'),to_date('2013-05-23','yyyy-mm-dd'),'b',900);

 

with t as
(select rownum rn,to_char(trunc(to_date('2012-01-01','yyyy-mm-dd'),'d')+rownum*7+1,'yyyy-mm-dd') time1,
to_char(trunc(to_date('2012-01-01','yyyy-mm-dd'),'d')+(rownum+1)*7,'yyyy-mm-dd') time2
from all_objects where rownum<=100)
select case when '2013-05-17' between a.time1 and a.time2 then '2013-05-17' else a.time1 end
||'到'||case when '2013-06-23' between a.time1 and a.time2 then '2013-06-23' else a.time2 end 日期范围,b."desc" 指标描述,
nvl(sum(case when to_char(b.startdate,'yyyy-mm-dd') between a.time1 and a.time2 then b."value" end),0) 指标值
from
(select time1,time2 from t where rn between
(select rn from t where '2013-05-17' between time1 and time2)
and
(select rn from t where '2013-06-23' between time1 and time2)) a left join test b
on 1=1
group by a.time1,a.time2,b."desc"
order by a.time1,"desc"
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式