Select for max date of the Inserted dates on every month - oracle

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);

Related

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').

Get Distinct Column Count By Day

I'm using Oracle 12c and I need to get the count of unique username rows for each day. Currently the table below has multiple rows with the same username on the same day (date_created).
LOGIN_HISTORY
----------------
id (unique random generated long)
date_created (timestamp)
username (var char)
In the query below I group using the day, month, and year, which is required to uniquely identify each day your date_created column. I used COUNT(DISTINCT username) to identify the count of unique usernames for a given day.
SELECT TO_CHAR(date_created, 'DD') DAY,
TO_CHAR(date_created, 'MM') MONTH,
TO_CHAR(date_created, 'YYYY') YEAR,
COUNT(DISTINCT username) AS userCount
FROM LOGIN_HISTORY
GROUP BY TO_CHAR(date_created, 'DD'),
TO_CHAR(date_created, 'MM'),
TO_CHAR(date_created, 'YYYY')

Distinct the extract month for 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);

Oracle SQL display a different column depending on the date

I have a table that has 33 columns
Employee Month Day1 Day2 ...... etc. etc.
The day column represents the day of the month.
Is it possible to just display the Employee column and the Day of the month depending on today's date?
i.e.
12th May
Employee Day12
no idea where to start if it's possible. any help would be great
You get a record (here: month) with the WHERE clause. You get a certain column (here: day) with DECODE or CASE.
select
employee,
decode( extract(day from sysdate), 1, day1, 2, day2, 3, day3, ... ) as day
from mytable t
where t.month = extract(month from sysdate)
group by employee
order by employee;

Grouping by Fiscal Year (Oracle)

Is there a way in Oracle that can pull the FY? I used the script below to pull just two FY. Mytable date range is from FY1998 to FY2009.
SELECT 'FY2008' as FY,
Site,
COUNT(*)
FROM mytable
WHERE date >='10-OCT-2007'
AND date <'10-OCT-2008'
GROUP BY site
SELECT 'FY2008' as FY,
Site,
COUNT(*)
FROM mytable
WHERE date >='10-OCT-2008'
AND date <'10-OCT-2009'
GROUP BY site
Pulling two FY is OK but it's too much repeatative when pulling more than 10 FY.
Add 83 days to your date and truncate it to whole year:
select 'FY'||TRUNC(date + 83, 'YYYY') as FY, Site, count(*)
from mytable
group by 'FY'||TRUNC(date + 83, 'YYYY'), site
Assuming Oracle 9i+, use a CASE expression:
SELECT CASE
WHEN TO_CHAR(t.date, ) = 10 AND EXTRACT(DAY FROM t.date) >= 10 THEN
'FY' || EXTRACT(YEAR FROM t.date) + 1
WHEN TO_CHAR(t.date, ) > 10 THEN
'FY' || EXTRACT(YEAR FROM t.date) + 1
ELSE
'FY' || EXTRACT(YEAR FROM t.date)
END AS FY,
t.site,
COUNT(*)
FROM YOUR_TABLE t
GROUP BY t.site, FY
And for completeness, in addition to #eumiro answer. In countries (such as Australia) which have a financial year running from 1 July to 30 June, you can replace the 83 with 184.
A few options:
You can use the to_char function here. Check this link for an explanation:
http://www.techonthenet.com/oracle/functions/to_char.php
You may also try using a case statement
select case when date >='10-OCT-2007' and date <'10-OCT-2008' then 'FY08'
when date >='10-OCT-2008' and date <'10-OCT-2009' then 'FY09'
else 'Other' end as fiscal_year, count(*)
from mytable
group by case when date >='10-OCT-2007' and date <'10-OCT-2008' then 'FY08'
when date >='10-OCT-2008' and date <'10-OCT-2009' then 'FY09'
else 'Other' end
Ultimately, if you have create table privileges you may want to consider making a date lookup table. Search for "date dimension" in data warehousing guides.
For example:
Your table would have
date, date_desc, fiscal_year, etc....
then you could just join and group by fiscal year, or whatever else you want.
Here is another way to easily determine the Fiscal Year of a date for those who's Fiscal Year runs from July to June:
SELECT 'FY'||TO_CHAR(ROUND(your_date_here,'YEAR'),'YY') AS FY

Resources