oracle 用于获取过去 30 天数据的 SQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5210817/
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-10 03:07:56  来源:igfitidea点击:

SQL Query to fetch data from the last 30 days?

oracledatetime

提问by miracle

Hi I am new to Oracle. How do I do a simple statement, for example get product id from the last 30, or 20 days purchase date?

您好,我是 Oracle 的新手。我如何做一个简单的陈述,例如从最近 30 天或 20 天的购买日期获取产品 ID?

SELECT productid FROM product
WHERE purchase_date ? 

回答by jgrabowski

SELECT productid FROM product WHERE purchase_date > sysdate-30

回答by Rajesh Chamarthi

The easiest way would be to specify

最简单的方法是指定

SELECT productid FROM product where purchase_date > sysdate-30;

SELECT productid FROM product where purchase_date > sysdate-30;

Remember this sysdate above has the time component, so it will be purchase orders newer than 03-06-2011 8:54 AM based on the time now.

请记住,上面的 sysdate 具有时间组件,因此根据现在的时间,它将是比 03-06-2011 8:54 AM 更新的采购订单。

If you want to remove the time conponent when comparing..

如果要在比较时删除时间分量..

SELECT productid FROM product where purchase_date > trunc(sysdate-30);

And (based on your comments), if you want to specify a particular date, make sure you use to_date and not rely on the default session parameters.

并且(根据您的评论),如果您想指定特定日期,请确保使用 to_date 而不是依赖默认会话参数。

SELECT productid FROM product where purchase_date > to_date('03/06/2011','mm/dd/yyyy')

SELECT productid FROM product where purchase_date > to_date('03/06/2011','mm/dd/yyyy')

And regardng the between (sysdate-30) - (sysdate) comment, for orders you should be ok with usin just the sysdate condition unless you can have orders with order_dates in the future.

关于 (sysdate-30) - (sysdate) 之间的评论,对于订单,您应该只使用 sysdate 条件就可以了,除非您将来可以使用 order_dates 进行订单。

回答by Gabriele D'Antona

Pay attention to one aspect when doing "purchase_date>(sysdate-30)": "sysdate" is the current date, hour, minute and second. So "sysdate-30" is not exactly "30 days ago", but "30 days ago at this exact hour".

做“purchase_date>(sysdate-30)”时要注意一个方面:“sysdate”是当前的日期、时、分、秒。所以“sysdate-30”不完全是“30 天前”,而是“30 天前的这个时间点”。

If your purchase dates have 00.00.00 in hours, minutes, seconds, better doing:

如果您的购买日期在小时、分钟、秒中有 00.00.00,最好这样做:

where trunc(purchase_date)>trunc(sysdate-30)

(this doesn't take hours, minutes and seconds into account).

(这不需要考虑小时、分钟和秒)。

回答by Faisal

Try this : Using this you can select data from last 30 days

试试这个:使用它你可以选择过去 30 天的数据

SELECT
    *
FROM
    product
WHERE
    purchase_date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)

回答by sinethr

SELECT COUNT(job_id) FROM jobs WHERE posted_date < NOW()-30;

Now()returns the current Date and Time.

Now()返回当前日期和时间。

回答by srinivas

select status, timeplaced 
from orders 
where TIMEPLACED>'2017-06-12 00:00:00'