现有如下需求。
用户输入条件,假如2013-1-1 到 2013-1-10,那我就先二话不说查询10条数据出来。
(是根据用户输入的时间段来显示多少条)
我是这样做的.
--将条件保存临时表内
INSERT into tablename11(ssid) select sj FROM (
select sday + level - 1 sj
from (select to_date('2013-4-1', 'yyyy-mm-dd') sday,
to_date('2013-5-30', 'yyyy-mm-dd') eday
from dual) t connect by level <= t.eday - t.sday + 1);建立一张临时表,循环插入该时间段的数据,再与我数据表进行关联。 SELECT t.ssid,COUNT(B.id) ,b.inf_id,to_char(b.last_udpate_date,'yyyy-mm-dd') FROM tablename11 t
LEFT JOIN ERP.T_BUS_OPERATIONS B
ON to_char(t.ssid,'yyyy-MM-dd')=TO_CHAR(b.create_date,'yyyy-MM-dd')
and ((B.last_udpate_date-B.create_date)*24*60>11or (B.last_udpate_date-B.create_date)*24*60<11)
and b.inf_id='014' GROUP BY t.ssid,b.inf_id,to_char(b.last_udpate_date,'yyyy-mm-dd')
ORDER BY t.ssid;但是我查询出来的结果不是用户想要的。这里5月7号是创建时间,代表这一天有11条数据,它们的修改时间是 5月15.
那我查出来的结果应该是这样2013-5-7 11
2013-5-8 11
2013-5-9 11
2013-5-10 11
2013-5-11 11
2013-5-12 11
2013-5-13 11
2013-5-14 11
2013-5-15 11 -- 15为它的最后修改时间 所以到15号结束但是表里面还可能有重复的数据 比如。
2013/4/24 这天有3条数据,它们最后修改时间也是2013-05-15那我查询的结果应该是为
2013/4/24 3
2013/4/25 3
2013/4/26 3
2013/4/27 3
2013/4/28 3
2013/4/29 3
2013/5/1 3
2013/5/2 3
2013/5/3 3
2013/5/4 3
2013/5/5 3
2013/5/6 3
2013/5/7 14 --因为7号那天有11条数据了,所以要加上就是14条了
2013/5/8 14
2013/5/9 14
2013/5/10 14
2013/5/11 14
2013/5/12 14
2013/5/13 14
2013/5/14 14
2013/5/15 14 -- 一直到修改时间为准不知道大家能否理解,找到规律没有。求大神指点。
SQL需求
用户输入条件,假如2013-1-1 到 2013-1-10,那我就先二话不说查询10条数据出来。
(是根据用户输入的时间段来显示多少条)
我是这样做的.
--将条件保存临时表内
INSERT into tablename11(ssid) select sj FROM (
select sday + level - 1 sj
from (select to_date('2013-4-1', 'yyyy-mm-dd') sday,
to_date('2013-5-30', 'yyyy-mm-dd') eday
from dual) t connect by level <= t.eday - t.sday + 1);建立一张临时表,循环插入该时间段的数据,再与我数据表进行关联。 SELECT t.ssid,COUNT(B.id) ,b.inf_id,to_char(b.last_udpate_date,'yyyy-mm-dd') FROM tablename11 t
LEFT JOIN ERP.T_BUS_OPERATIONS B
ON to_char(t.ssid,'yyyy-MM-dd')=TO_CHAR(b.create_date,'yyyy-MM-dd')
and ((B.last_udpate_date-B.create_date)*24*60>11or (B.last_udpate_date-B.create_date)*24*60<11)
and b.inf_id='014' GROUP BY t.ssid,b.inf_id,to_char(b.last_udpate_date,'yyyy-mm-dd')
ORDER BY t.ssid;但是我查询出来的结果不是用户想要的。这里5月7号是创建时间,代表这一天有11条数据,它们的修改时间是 5月15.
那我查出来的结果应该是这样2013-5-7 11
2013-5-8 11
2013-5-9 11
2013-5-10 11
2013-5-11 11
2013-5-12 11
2013-5-13 11
2013-5-14 11
2013-5-15 11 -- 15为它的最后修改时间 所以到15号结束但是表里面还可能有重复的数据 比如。
2013/4/24 这天有3条数据,它们最后修改时间也是2013-05-15那我查询的结果应该是为
2013/4/24 3
2013/4/25 3
2013/4/26 3
2013/4/27 3
2013/4/28 3
2013/4/29 3
2013/5/1 3
2013/5/2 3
2013/5/3 3
2013/5/4 3
2013/5/5 3
2013/5/6 3
2013/5/7 14 --因为7号那天有11条数据了,所以要加上就是14条了
2013/5/8 14
2013/5/9 14
2013/5/10 14
2013/5/11 14
2013/5/12 14
2013/5/13 14
2013/5/14 14
2013/5/15 14 -- 一直到修改时间为准不知道大家能否理解,找到规律没有。求大神指点。
SQL需求
解决方案 »
- 一条SQL语句能得到这样的分组结果吗?
- oracle 存储过程 性能调优
- 两个库中相同用户名下的表数据比对
- 如何在sql输出结果前加标题?
- 家里的计算机正在运行Oracle,我不小心把电源拔了。计算机重新启动后似乎还能够运行Oracle,怎么看Oracle有没有损坏?为了Oracle能够在家
- 求大神 PLSQL 使用
- 简单存储过程问题
- 这个建表语句怎么会错呢?请帮帮我
- 请问在ORACLE中如何实现SELECT TOP 10 * FROM 表名
- 安装oracle时老出错,谁来指点一下
- Find the smallest collection of indexes that speed up the processing of all SELECT statements
- 在关联查询视图表和本地表的时候报无法从套接字读取更多的数据
不然如何下手啊
上面的语句是统计的语法,请帮忙修改
with t1 as
(
select date'2013-05-03' c1,3 c2 from dual union all
select date'2013-05-07' c1,11 c2 from dual union all
select date'2013-05-09' c1,5 c2 from dual
)select t.tdate,nvl(sum(c2) over(order by t.tdate),0) sc
from
(
select date'2013-05-01'+level-1 tdate
from dual
connect by level <= date'2013-05-10'-date'2013-05-01'+1
) t left join t1 on t.tdate = t1.c1
tdate sc
---------------------------------
1 2013/5/1 0
2 2013/5/2 0
3 2013/5/3 3
4 2013/5/4 3
5 2013/5/5 3
6 2013/5/6 3
7 2013/5/7 14
8 2013/5/8 14
9 2013/5/9 19
10 2013/5/10 19