在 Oracle 中使用 sql 获取过去 7 天的数据行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27859147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:09:04  来源:igfitidea点击:

Get data rows for past 7 days using sql in Oracle

sqloracleoracle-sqldeveloper

提问by USSR

Struggling with the query which returns data rows including some rows for the past 7 days. There are lots of left joins tables and looks like I've missed something. here is an example of my code so far:

查询返回数据行,包括过去 7 天的一些行。有很多左连接表,看起来我错过了一些东西。到目前为止,这是我的代码示例:

select a.a,
b.b,
c.c,
d.d,
e.e,
f.f,
s.start_date,
ack.acknowledge_date,
fin.finish_date, 
g.g,
h.h, 
i.i 
from table_a a, 
inner join table_b b on ... = ..., 
left join table_c c on ... = ..., 
left join table_d d on ... = ... ` 

And so on.

等等。

I would like to get data rows (if any!) from columns s.start_date, ack.acknowledge_date and Fin.finish_date for the last 7 days (I suggest it is (sysdate -7)). However, if I do the following:

我想从过去 7 天的 s.start_date、ack.acknowledge_date 和 Fin.finish_date 列中获取数据行(如果有的话!)(我建议它是 (sysdate -7))。但是,如果我执行以下操作:

select a.a,
b.b,
c.c,
...,
...,
...,
from ...
inner join ...
on ... = ...
left join ...
on ... = ...
left join`...
on ... = ...

where s.start_date >= sysdate -7 and 
ack.acknowledge_date >= sysdate -7 and 
fin.finish_date >= sysdate -7;

Then I have 0 rows in returns. But ideally query should return all rows from all tables and IF any rows on these date fields it should return them too, if there is no data in these date fields then leave it empty but all other rows should return as normal.

然后我有 0 行回报。但理想情况下,查询应该返回所有表中的所有行,如果这些日期字段上有任何行,它也应该返回它们,如果这些日期字段中没有数据,则将其留空,但所有其他行应正常返回。

回答by jarlh

Don't put OUTERtable conditions in the WHEREclause if it's an OUTER JOIN. Move them to the ONclause instead!

不要把OUTER表条件的WHERE条款,如果它是一个OUTER JOIN。将它们移到ON子句中!

LEFT JOIN <...> ON <...> = <...>
               AND s.start_date >= sysdate -7

回答by Matt

Use ORinstead of AND

使用OR代替AND

WHERE s.start_date >= sysdate -7
      OR ack.acknowledge_date >= sysdate -7
      OR fin.finish_date >= sysdate -7;