SQL 如何从第一次约会开始从 ORACLE 获取当月记录。?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22060885/
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
How to get current month records from ORACLE since first date.?
提问by Priyan RockZ
here is my code
这是我的代码
SELECT h.ENTERED_DATE,
d.DENOMINATION,
sum(h.invoice_total-h.TOTTAL_DISCOUNT)as amount
FROM sales_details d
LEFT JOIN sales_header h
ON d.invoice_id=h.invoice_id
WHERE entered_by='2254'
--HERE IS NEED TO GET DETAILS CURRENT MONTH 1st Date to Sysdate
GROUP BY
ENTERED_DATE,
d.DENOMINATION
ORDER BY
entered_date,
denomination
- In my application just send only sysdateas parameter. no need to SYSDATE-30. need 1st date to SYSDATE
- 在我的应用程序中,只发送sysdate作为参数。 不需要 SYSDATE-30。需要第一个日期到 SYSDATE
here shows my two tables
sales_header table
这里显示了我的两个表
sales_header 表
sales_details table
sales_details 表
回答by i100
try this:
尝试这个:
WHERE entered_by='2254'
AND ENTERED_DATE BETWEEN trunc (sysdate, 'mm')/*current month*/ AND SYSDATE
回答by Mudassir Hasan
Compare months in where condition
比较在 where 条件下的月份
WHERE to_char( sysdate, 'mm' ) = to_char( ENTERED_DATE, 'mm' )
WHERE to_char( sysdate, 'mm' ) = to_char( ENTERED_DATE, 'mm' )
Also
还
WHERE EXTRACT(month FROM sysdate)=EXTRACT(month FROM ENTERED_DATE)
WHERE EXTRACT(month FROM sysdate)=EXTRACT(month FROM ENTERED_DATE)
回答by Tony L.
Mudassir's answer worked for me but I would also check the year to make sure you are not getting last year's records.
Mudassir 的回答对我有用,但我也会检查年份以确保您没有获得去年的记录。
WHERE TO_CHAR(ENTERED_DATE, 'mm') = TO_CHAR(SYSDATE, 'mm')
AND TO_CHAR(ENTERED_DATE, 'yyyy') = TO_CHAR(SYSDATE, 'yyyy')
Also, if you are running a large query, functions in your WHERE
clause can slow it down. If so, you may want to consider a function based index.
此外,如果您正在运行大型查询,则WHERE
子句中的函数可能会减慢查询速度。如果是这样,您可能需要考虑基于函数的索引。
回答by CloudyMarble
Try :
尝试 :
SELECT TRUNC(SYSDATE, 'month') FROM DUAL;
回答by Johan Durán
select 'WITHIN' as result
from dual
where '28-FEB-2019' between trunc(last_day(add_months(sysdate, -1))+1) and trunc(last_day(sysdate));
Take care because there are many examples that are using just the month and because of that you could get records from all the year, year is not being considered. Also as you can see I am using trunc it is to avoid timing inconsistencies and just use the date.
请注意,因为有许多示例仅使用月份,因此您可以获得全年的记录,不考虑年份。此外,正如您所看到的,我使用 trunc 是为了避免时间不一致并仅使用日期。