I have the below select query for daily data, how do I amend to make it pull the full month of data
select *
from BAU_SDT_INV_USER
where trunc(CHECKED_DATE) = to_date('2018/12/18', 'YYYY/MM/DD')
If you want the full month in Oracle:
where checked_date >= date '2018-12-01' and
checked_date < date '2019-01-01'
Maybe something like this?
select * from BAU_SDT_INV_USER
where CHECKED_DATE >= to_date('2018/12/01', 'YYYY/MM/DD')
and CHECKED_DATE < to_date('2019/01/01', 'YYYY/MM/DD')
If performance is not a key point for your query, just trunc by month on both ends of the comparison :
select *
from BAU_SDT_INV_USER
where trunc(CHECKED_DATE, 'mm') = trunc( to_date('2018/12/18', 'YYYY/MM/DD'), 'mm')
select *
from BAU_SDT_INV_USER
where trunc(CHECKED_DATE) BETWEEN to_date('2018/12/01', 'YYYY/MM/DD')
AND to_date('2018/12/31', 'YYYY/MM/DD')
More Index Friendly approach would be
select *
from BAU_SDT_INV_USER
where CHECKED_DATE between ADD_MONTHS(sysdate, -1) and sysdate
EDIT Please note that between clause is inclusive. you can also use
select *
from BAU_SDT_INV_USER
where CHECKED_DATE >= ADD_MONTHS(to_date(:inputDate, 'YYYY/MM/DD'), -1)
and CHECKED_DATE < TO_DATE(:input_Date,'YYYY/MM/DD');
Related
I need help with my query
select distinct count(item_number), creation_date
from EGP_SYSTEM_ITEMS_B ,
all I need to count the item number every month
for example
3-9-2020 count:29700
4-9-2020 count:29600
5-9-2020 Count:30000
and get the all date for the month and the previous month from creation_id or sysdate any of them
thanks
To count the number per month, you would aggregate by the month:
select trunc(creation_date, 'MON') as yyyymm, count(*)
from EGP_SYSTEM_ITEMS_B
group by trunc(creation_date, 'MON');
Your question is not entirely clear; perhaps you're trying to get
select TRUNC(CREATION_DATE), count(item_number)
from EGP_SYSTEM_ITEMS_B
WHERE TRUNC(CREATION_DATE)
IN (ADD_MONTHS(TRUNC(SYSDATE), -1),
TRUNC(SYSDATE),
ADD_MONTHS(TRUNC(SYSDATE), 1))
GROUP BY TRUNC(CREATION_DATE)
EDIT
Apparently OP wants a running monthly count, so something like:
WITH cteLimits (START_DATE, END_DATE)
AS (SELECT ADD_MONTHS(TRUNC(SYSDATE), -2), ADD_MONTHS(TRUNC(SYSDATE), -1) - INTERVAL '1' DAY FROM DUAL UNION ALL
SELECT ADD_MONTHS(TRUNC(SYSDATE), -1), TRUNC(SYSDATE) - INTERVAL '1' DAY FROM DUAL UNION ALL
SELECT TRUNC(SYSDATE), ADD_MONTHS(TRUNC(SYSDATE), 1) - INTERVAL '1' DAY FROM DUAL),
cteDay_totals
AS (SELECT TRUNC(CREATION_DATE) AS CREATION_DATE,
COUNT(*) AS DAY_TOTAL
FROM EGP_SYSTEM_ITEMS_B
GROUP BY TRUNC(CREATION_DATE))
SELECT l.START_DATE,
l.END_DATE,
SUM(d.DAY_TOTAL) AS MONTH_TOTAL
FROM cteLimits l
INNER JOIN cteDay_totals d
ON d.CREATION_DATE BETWEEN l.START_DATE AND l.END_DATE
GROUP BY l.START_DATE,
l.END_DATE
I have a few dates in the YYYY-MM-DD format in the table column. how can I find the nearest date, less than or equal to today?
Normally I would use:
<= (SELECT TO_CHAR (TRUNC (SYSDATE), 'YYYY-MM-DD') from DUAL)
but it will return all values less or equal, and I only want one, the nearest.
Thanks for help.
One method is:
select t.*
from (select t.*
from t
where datecol < sysdate -- trunc(sysdate) ???
order by datecol desc
) t
where rownum = 1;
How can I get the year and month of a date in the where clause using Oracle.
I used to be working with SQL server and it was as simple as YEAR(FIELDNAME) and MONTH(FIELDNAME).
I have tried the following:
SELECT *
FROM myschema.mytablename
WHERE EXTRACT(YEAR FROM myDATE) = 2017
however it gives ORA-30076 Error
Have you tried EXTRACT()?
SELECT EXTRACT(YEAR FROM DATE '2017-12-01') FROM DUAL;
2017
SELECT EXTRACT(MONTH FROM DATE '2017-12-01') FROM DUAL;
12
I tried this in sql fiddle with 11g and it works in WHERE clause too.
http://sqlfiddle.com/#!4/fb2b09/2
SELECT *
FROM myschema.mytablename
WHERE TO_CHAR(myDATE, 'YYYY') = '2017';
Explicitly convert year part of DATE into CHAR and compare it with literal.
For year and month comparison:
SELECT *
FROM myschema.mytablename
WHERE TO_CHAR(myDATE, 'YYYY') = '2017' AND TO_CHAR(myDate, 'MM') = '07';
Your query should work, but a better alternative would be
SELECT *
FROM YOUR_TABLE
WHERE MY_DATE BETWEEN TO_DATE('01-JAN-2017', 'DD-MON-YYYY')
AND TO_DATE('01-JAN-2018', 'DD-MON-YYYY') - INTERVAL '1' SECOND
Best of luck.
I did this with the function EXTRACT() and it works good for me.
I'm gonna share the query code here:
SELECT extract(YEAR from s.CREATE_DATE) as YEAR, extract(MONTH from s.CREATE_DATE) as MONTH, s.SALES_PERSON_GID, COUNT(s.SALES_ORDER_ID) as "TOTAL ORDERS" FROM SALES_ORDERS s, SALES_ORDER_STATUS ss WHERE s.SALES_ORDER_ID = ss.SALES_ORDER_ID and ss.STATUS_TYPE_ID = 'SALE ORDER STATUS' and ss.STATUS_VALUE_GID = 'SALE ORDER STATUS_DELIVERED' GROUP BY s.SALES_PERSON_GID,s.CREATE_DATE
I have a question regarding getting yesterday's data in oracle sql
my query is something like this:
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
FAB_ENTRY_DATE = TO_DATE(SYSDATE-1, 'MM/DD/YYYY')
The value in FAB_ENTRY_DATE is like this:
8/25/2014 3:03:11 PM
And it doesn't show any data when I execute the query, Please help me
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
to_date(FAB_ENTRY_DATE, 'MM/DD/YYYY') = TO_DATE(SYSDATE-1, 'MM/DD/YYYY')
[You should match only the date part]
or
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
FAB_ENTRY_DATE >= (SYSDATE - 1) and FAB_ENTRY_DATE < SYSDATE;
More efficient (if FAB_ENTRY_DATE has index - uses it):
SELECT SUM(CURRENT_WEIGHT)
FROM FABRICATION_HIST
WHERE FAB_ENTRY_DATE >= TRUNC(SYSDATE)-1
AND FAB_ENTRY_DATE < TRUNC(SYSDATE)
NOTE:
Don't use TO_DATE(SYSDATE, 'MM/DD/YYYY') to drop time part:
as it works only if default date format on database is 'MM/DD/YYYY', if not - you get errors like this:
ORA-01843: not a valid month : SELECT TO_DATE(SYSDATE, 'MM/DD/YYYY') FROM DUAL
You have equality. 8/25/2014 3:03:11 PM won't be equal to 8/25/2014
You need to specify a range:
WHERE FAB_ENTRY_DATE >= TO_DATE(SYSDATE-1, 'MM/DD/YYYY')
AND FAB_ENTRY_DATE < TO_DATE(SYSDATE, 'MM/DD/YYYY')
Try this:
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
TO_CHAR(FAB_ENTRY_DATE, 'MM/DD/YYYY') = TO_CHAR(SYSDATE-1, 'MM/DD/YYYY')
Since you don't need to get the sum on a span of dates (between two dates), you can compare the date that you want as varchar without the Time portion. "8/25/2014" = "8/25/2014"
you can trunc data column:
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
to_date(FAB_ENTRY_DATE, 'MM/DD/YYYY') = trunc(SYSDATE) - 1
see Oracle's documentation
How can I retrieve data on time range where tickets issued between the times of 6am - 9:15am for the past month.
I tried this but was wrong.Its in oracle and to_char is not function name in SQL. how can i do that in sql.
select *
from [ICPS].[dbo].[tickets]
where t_date_time_issued > sysdate - 30
and to_char(t_date_time_issed, 'hh24:mi:ss') >= '06:00:00'
and to_char(t_date_time_issued, 'hh24:mi:ss') <= '09:15:00'
Assuming that you want it in SQL-Server("Its in oracle and to_char is not function name in SQL"), you can cast the datetme to time:
SELECT *
FROM [dbo].[tickets]
WHERE t_date_time_issued > DATEADD(mm, - 30, GetDate())
AND Cast(t_date_time_issued as TIME) between '06:00' and '09:10'
Demo
You can use to_timestamp() in stead of to_char. Details about to_timestamp() is: http://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2118.htm#OLADM696
Hope will help!
try this:
SELECT
*
FROM
[ICPS].[DBO].[tickets]
WHERE
t_date_time_issued BETWEEN add_months(TRUNC(SYSDATE, 'MM'), - 1) AND (
TRUNC(SYSDATE, 'MM') - (1/24/60/60))
AND TO_CHAR(t_date_time_issed, 'HH24:MI:SS') >= '06:00:00'
AND TO_CHAR(t_date_time_issed, 'HH24:MI:SS') <= '09:15:00'
SELECT *
FROM [ICPS].[dbo].[tickets]
WHERE convert (datetime,t_date_time_issued,101)
between convert(datetime,'2013/11/01',101) and convert (datetime,'2013/12/04',101)
AND datepart(hour,t_date_time_issued) between '06' and '09'