Oracle SQL display a different column depending on the date - oracle

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;

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

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

Subtracting Dates ORA01847

I am a newbie to Oracle. I have been looking at the examples how to subtract between two days with to_Date.
However, every time I run this with to_date logic
SELECT (to_date((SELECT PAYMENT_DATE FROM INVOICES WHERE INVOICE_ID =3) - to_date((SELECT INVOICE_DUE_DATE FROM INVOICES WHERE INVOICE_ID= 3)))) FROM DUAL; I ended up getting this error: ORA-01847: day of month must be between 1 and last day of month
Payment Date is : 15-SEP-17 Invoice Due Date is: 24-JUN-17
Is there a simple way to calculate the difference in days between two columns?
Thanks in advance.
you don't need to convert with by using to_date without formatting like 'dd.mm.rrrr'. i.e. omit to_date clauses. Substracting dates yield a number ( assuming both column PAYMENT_DATE, INVOICE_DUE_DATE are in date type )
SELECT (SELECT PAYMENT_DATE FROM INVOICES WHERE INVOICE_ID =3) - (SELECT INVOICE_DUE_DATE FROM INVOICES WHERE INVOICE_ID= 3) INTO v_day FROM DUAL;
where v_day is a number. By the way, it's better to use in this way:
SELECT ( PAYMENT_DATE - INVOICE_DUE_DATE ) INTO v_day FROM INVOICES WHERE INVOICE_ID =3

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

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