Distinct the extract month for oracle - oracle

Below is my query to extract month from date,
Select distinct Extract (Month From Dates) As Bulan, Count(Matric_No) As Total,Matric_No
From Stud_Sick
Group By Matric_No,Dates
Order By Dates;
This give me error 'not a SELECTED expression'. Anyone know how to fix this?

I suspect you're after the following:
select extract(month from dates) bulan,
count(matric_no) as total,
matric_no
from stud_sick
group by extract(month from dates),
matric_no
order by extract(month from dates);
Or possibly you're after:
select extract(month from dates) bulan,
count(matric_no) as total
from stud_sick
group by extract(month from dates)
order by extract(month from dates);

Related

Select for max date of the Inserted dates on every month

I have a sales table It includes each sale date in the date format in the sales_day column. I need to find the maximum date for each month from these entered dates. I am using oracle db
You can group by month and use the max aggregation:
select extract(month from sale_day) as month, max(sale_day) maxdate
from sales
group by extract(month from sale_day);
Fiddle
Note that this will get dates from all the years, which might not be what you want.
If you want to limit the rows to a certain year:
select extract(month from sale_day) as month, max(sale_day) maxdate
from sales
where sale_day between TO_DATE('2021-01-01','YYYY-MM-DD') and
TO_DATE('2021-12-31','YYYY-MM-DD')
group by extract(month from sale_day);

how to get year and month into number oracle

i'm trying to get sysdate year and month into number or char.
I'd want something like this:
202107
or last month
202106
i tried this code:
select trunc(add_months(sysdate, -1), 'MM') from dual;
please help, thanks
Convert it to a string and then to a number:
SELECT TO_NUMBER(TO_CHAR(ADD_MONTHS(SYSDATE,-1), 'YYYYMM'))
FROM DUAL;
Or, you can use extract and wrap it in a sub-query so you do not need to repeat adding the months:
SELECT EXTRACT(YEAR FROM dt) * 100 + EXTRACT(MONTH FROM dt)
FROM (
SELECT ADD_MONTHS(SYSDATE, -1) AS dt
FROM DUAL
)
Like
SELECT EXTRACT(year from sysdate) * 100 + EXTRACT(month from sysdate)
If you want to scroll around, manipulate the date before you extract from it, rather than minusing after you extract (gets tricky to e.g. go back a month if the date is in jan)
--will work for jan 2021
SELECT EXTRACT(year from ADD_MONTHS(somedate, -1)) * 100 + EXTRACT(month from ADD_MONTHS(somedate, -1))
--won't work for jan 2021
SELECT EXTRACT(year from somedate) * 100 + (EXTRACT(month from somedate) - 1)
TRUNC is a device that "rounds" a date to a particular interval, such as "trimming the time off a datetime" or "making any day of the week back to the date that was the start of the week" - very useful for all sorts of stuff like "total sales by month - SUM(sale) GROUP BY TRUNC(saledate, 'mm')" but it keeps all the components of the date (the day, the hour, the minute etc) so it isn't what you want.

Count of Records and Average of count of records Query - Oracle

Output like:
Year -> Month -> Product Name -> Total count of records for the year -> Average count of records for the year
My query returns the total and average for the whole year, I would like it to be broken down by month as well:
SELECT PRODUCT_ID,PRODUCT_NAME,EXTRACT(YEAR FROM DATE), COUNT(ID) AS TOTAL_COUNT,COUNT(ID)/COUNT(DISTINCT(EXTRACT(DATE))) AS AVG_NO_RECORDS
FROM TABLE
GROUP BY PRODUCT_ID,PRODUCT_NAME,EXTRACT(YEAR FROM DATE)
Apart from the fact that you used bunch of invalid things in this sample query (table is invalid table name, date is invalid column name, one of extracts is wrong), you'd use TO_CHAR on the date column with desired format mask - such as mm - and group by it as well. Something like this:
SELECT PRODUCT_ID,
PRODUCT_NAME,
EXTRACT(YEAR FROM DATE),
--
to_char(date, 'mm') as month, --> this ...
--
COUNT(ID) AS TOTAL_COUNT,
COUNT(ID)/COUNT(DISTINCT(EXTRACT(DATE))) AS AVG_NO_RECORDS
FROM TABLE
GROUP BY PRODUCT_ID,
PRODUCT_NAME,
EXTRACT(YEAR FROM DATE),
to_char(date, 'mm') --> ... and this
On the other hand, if you're already grouping by year and month, see if you can use a single column for that, e.g. to_char(date_column, 'mm.yyyy').

Issue with Case and Extract Function

I am writing a SELECT Query as below
SELECT COUNT(*) AS Number_of_rows
FROM PAY_DETL
WHERE
CASE
WHEN
Extract(MONTH FROM CURRENT_DATE) > 9
THEN
Extract(YEAR FROM PAY_EVT_BGN_DT) = Extract(YEAR FROM CURRENT_DATE) AND Extract(MONTH FROM CURRENT_DATE) > 9;
Where i am getting below error
ORA-00905: missing keyword
00905. 00000 - "missing keyword
statement where i am getting error is Extract(YEAR FROM PAY_EVT_BGN_DT "here")
Could someone please help me with above?
Thanks
A query nor a comment you posted help much; what does "not assign" mean?
Anyway: you can't use a conditional WHERE clause that way. A simple option is to split the query into two parts and UNION them, having the "distinguishing" parts of the WHERE clause opposite.
For example:
select count(*) as number_of_rows
from pay_detl
where extract(year from pay_evt_bgn_dt) = extract(year from current_date)
and extract(month from current_date) > 9 --> this condition ...
union
select count(*) as number_of_rows
from pay_detl
where condition_different_from_the_one_above
and extract(month from current_date) <= 9 --> ... is just the opposite from this one
If it doesn't help, please, modify the question, provide test case so that we'd see the input and describe what would the output look like (based on that input).
We must differentiate the two main conditions <,> 9. Then, there is to account for each case:
SELECT SUM(CASE WHEN Extract(MONTH FROM SYSDATE) > 9 THEN
DECODE(Extract(YEAR FROM PAY_EVT_BGN_DT), Extract(YEAR FROM CURRENT_DATE), 1, 0)
END COUNT_MORE_NINE),
SUM(CASE WHEN Extract(MONTH FROM SYSDATE) <= 9 THEN
1
END COUNT_ANOTHER)
FROM PAY_DETL;

Convert Numeric format dates to dates in Oracle

This is how my Date column looks like
RPT_DT (Data Type is Number)
20180131
20180130
20180129
I wanna extract month out of these dates(either Month or mm), and I tried below
select extract(month from to_date(Rpt_dt))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
I am getting the error "Not a valid month"
if there is not any particular reason to have a double conversion I would suggest you to handle the problem with this simple query:
select substr(to_char(RPT_DT),5,2)from THE_TABLE
this query should be more performant since it make only one conversion. in your sample you transform:
a number to a date
then a date to a char
the char again in date
finally you extract the month
let me know if it help
r.
try this,
SELECT EXTRACT(MONTH FROM TO_DATE(rpt_dt, 'YYYYMMDD'))
FROM TABLE_NAME;
and I believe you need to modify your query as you did not put the format 'MM/DD/YYYY',
select extract(month from to_date(Rpt_dt, 'MM/DD/YYYY'))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
This back-and-forth conversion is useless. Try this
select
extract(month from to_date(RPT_DT,'yyyymmdd'))
from TABLE_NAME;

Resources