查询符合这样条件的数据,日期大于某天,并且每天的字段f1都大于0

ahking 2009-03-17 03:19:39
查询符合这样条件的数据,日期大于某天,并且每天的字段f1都大于0
表里有日期、f1等字段
...全文
487 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlzfc 2009-03-18
  • 打赏
  • 举报
回复

SELECT
f1,
SUM(f1) OVER(PARTITION BY
n ) HJ
FROM talbe
WHERE
RQ > to_date('2009-01-02','yyyy-mm-dd')
and f1>0


minus

SELECT
f1,
SUM(f1) OVER(PARTITION BY
n ) HJ
FROM talbe
WHERE
RQ > to_date('2009-01-02','yyyy-mm-dd')
and f1 <=0
zlzfc 2009-03-18
  • 打赏
  • 举报
回复



SELECT
f1,
SUM(f1) OVER(PARTITION BY
n ) HJ
FROM talbe
WHERE
RQ > to_date('2009-01-02','yyyy-mm-dd')
and f1>0


minus

SELECT
SELECT
f1,
SUM(f1) OVER(PARTITION BY
n ) HJ
FROM talbe
WHERE
RQ > to_date('2009-01-02','yyyy-mm-dd')
and f1<=0
fosjos 2009-03-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ahking 的回复:]
设数据库结构是这样:
n f rq
a1 -1 2009-3-1
a1 1 2009-3-2
a2 2 2009-3-1
a2 3 2009-3-2

假设日期是3月1号以后的,a2可以获取到,a1就不可以
[/Quote]
早就可以这样出题了^_^
能说明下数据量和分布就更好了

索引有没有建过?
vc555 2009-03-18
  • 打赏
  • 举报
回复
select * from t a
where rq>...
and not exists(select 1 from t b
where b.rq>同上面
and b.f<0
and a.n=b.n);

或者
select * from t a
where rq>...
and not exists(select 1 from t b
where b.f<0
and a.n=b.n);

用not in或not exists没关系。如果慢,看索引如何建的?
如果F区分度好,在rq和f上分别建索引,第二个SQL即可。
否则建rq,f联合索引,用第一个sql.
ahking 2009-03-18
  • 打赏
  • 举报
回复
设数据库结构是这样:
n f rq
a1 -1 2009-3-1
a1 1 2009-3-2
a2 2 2009-3-1
a2 3 2009-3-2

假设日期是3月1号以后的,a2可以获取到,a1就不可以
fosjos 2009-03-18
  • 打赏
  • 举报
回复
日期,f1建个索引

如果还是很慢,说说具体数据量和分布
vc555 2009-03-18
  • 打赏
  • 举报
回复
LZ列出你的表结构,和你想要的查询结果。举个例子,都以具体数据展现出来。
ahking 2009-03-18
  • 打赏
  • 举报
回复
??
ahking 2009-03-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liulongjin2008 的回复:]
select *
from table_name b
where 日期>to_date('2009-03-02 00:00:00','yyyy-mm-dd hh24:mm:ss')
and not exists (select 'X' from table_name a from a.日期=b.日期 and f1 <=0 )
[/Quote]
速度不是一般的慢

设数据库结构是这样:
n f rq

要求这样的查询结果,rq大于某一天,n对应的f每天都大于0,得出这样的记录
liqiaolq 2009-03-17
  • 打赏
  • 举报
回复
学习下
liulongjin2008 2009-03-17
  • 打赏
  • 举报
回复
select *
from table_name b
where 日期>to_date('2009-03-02 00:00:00','yyyy-mm-dd hh24:mm:ss')
and not exists (select 'X' from table_name a from a.日期=b.日期 and f1<=0 )
ahking 2009-03-17
  • 打赏
  • 举报
回复
不好意思,可能是我没说清楚,只要是f1在这个时期内有一条不大于0,那么这条记录就不要了
Andy__Huang 2009-03-17
  • 打赏
  • 举报
回复
select *
from tb
where f1>0 and 日期>to_date('2009-03-02 00:00:00','yyyy-mm-dd hh24:mm:ss')
;

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧