First_Day/Last_Day 查询
First_Day/Last_Day Query
能否请您帮我更正以下脚本:
set LastDay=SELECT LAST_DAY(SYSDATE) FROM dual;
set FirstDay=Select trunc((sysdate),'month') as First_day_of_month from dual;
SELECT count(*) FROM tab1 g , h.ab1 LEFT JOIN tab2 h ON g.bba = h.bba
WHERE 1 = 1
AND g.DATE_ BETWEEN TO_DATE('FirstDay', 'YYYYMMDD') AND TO_DATE('LastDay', 'YYYYMMDD');
就是这样
select count(*)
from tab1 g left join tab2 h on g.bba = h.bba
and g.date_ between trunc(sysdate, 'month') and last_day(sysdate);
不是吗?
在 Oracle 中,DATE
ALWAYS 具有年、月、日、小时、分钟和秒组件。使用 LAST_DAY(SYSDATE)
仅设置日期的 year-month-day 部分并且不会修改时间部分,因此如果您从月初过滤到 LAST_DAY(SYSDATE)
那么您将排除最后一个值具有当前时间和 23:59:59.
之间的时间分量的月份中的第几天
你要用的是:
SELECT count(*)
FROM tab1 g
CROSS JOIN h.ab1 -- your query is confusing around the joins
-- and may need fixing
LEFT JOIN tab2 h
ON g.bba = h.bba
WHERE g.DATE_ >= TRUNC(SYSDATE, 'MM')
AND g.DATE_ < ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1);
能否请您帮我更正以下脚本:
set LastDay=SELECT LAST_DAY(SYSDATE) FROM dual;
set FirstDay=Select trunc((sysdate),'month') as First_day_of_month from dual;
SELECT count(*) FROM tab1 g , h.ab1 LEFT JOIN tab2 h ON g.bba = h.bba
WHERE 1 = 1
AND g.DATE_ BETWEEN TO_DATE('FirstDay', 'YYYYMMDD') AND TO_DATE('LastDay', 'YYYYMMDD');
就是这样
select count(*)
from tab1 g left join tab2 h on g.bba = h.bba
and g.date_ between trunc(sysdate, 'month') and last_day(sysdate);
不是吗?
在 Oracle 中,DATE
ALWAYS 具有年、月、日、小时、分钟和秒组件。使用 LAST_DAY(SYSDATE)
仅设置日期的 year-month-day 部分并且不会修改时间部分,因此如果您从月初过滤到 LAST_DAY(SYSDATE)
那么您将排除最后一个值具有当前时间和 23:59:59.
你要用的是:
SELECT count(*)
FROM tab1 g
CROSS JOIN h.ab1 -- your query is confusing around the joins
-- and may need fixing
LEFT JOIN tab2 h
ON g.bba = h.bba
WHERE g.DATE_ >= TRUNC(SYSDATE, 'MM')
AND g.DATE_ < ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1);