SQL Function to select first day of current year and everyday after - sql

I'm looking to get a quick and efficient code to get the first day of current year and everyday after that and their week numbers. So far I have something like this to start me off:
SELECT
to_char(TRUNC(SysDate,'YEAR'), 'WW')Week_No,
to_char(TRUNC(SysDate,'YEAR'), 'DD/MM/YY')First_Day
FROM dual;
Which gets me week number and first day of current year. I also have this to get me the last calendar day of current year:
SELECT ADD_MONTHS(trunc(sysdate,'YEAR'),12)-1 from dual;
I am being extremely silly or maybe it's end of the day brain mush but I'm trying to get the bit in the middle now. The point of this is to get a list of each calendar day and week number.
If anyone can guide me here I'd appreciate this.
Thanks

Are you looking for something like this:
SELECT
ST_DT + LEVEL - 1,
TO_CHAR(ST_DT + LEVEL - 1, 'IW') AS DY
FROM
(
SELECT
TRUNC(SYSDATE, 'YEAR') ST_DT,
TRUNC(SYSDATE, 'YEAR') + INTERVAL '12' MONTH - INTERVAL '1' DAY END_DT
FROM DUAL
)
CONNECT BY LEVEL <= END_DT - ST_DT
ORDER BY LEVEL
Cheers!!

Related

Impala - Grouping By Week

This is the example of the data i have:
This is the output i need(grouping the total number of logins by week(sunday to saturday):
I tried couple of queries but it did not worked out. Thanks for helping me. I am using Impala for this.
You can use simple trunc() to get a monday of the week and then use it to group by the data.
select trunc(login_dt, 'DAY') - interval '1' day as Sunday_of_week,
sum(num_of_logins) total_logins
from mytable)rs
group by 1
trunc(dt, 'DAY') - Returns starting day of the week (Monday)
hive only solution -
select next_day(date_sub(dt, 7), 'SUN') as Sunday_of_week,
sum(l) total_logins
from t rs where dt is not null
group by next_day(date_sub(dt, 7), 'SUN') ;
output -

How do get data from the beginning of five fiscal quarters ago

I am tasked with getting data from beginning five fiscal quarters ago until present. This was not easy for an oracle novice like myself. Our fiscal year begins 1 November so this posed another dimension. Anyway, this code seems to work just fine. Just thought I would share and who knows, maybe you will find a mistake or better way.
Few observations.
ADD_MONTHS(sysdate - interval '2' year, -3) can be simplified to sysdate - interval '2-3' year to month.
TRUNC(date) gives a date output. So no need to use TO_DATE function on it.
I think ADD_MONTHS(sysdate - interval '2' year, +1) is wrong. It should be simply sysdate - interval '2' year. You can check by substituting
sysdate with a date in Decemeber, date'2013-12-04'.
Simplified query
select trunc(sysdate - interval '2-3' year to month,'Q') as beg_qtr,
trunc(sysdate - interval '2' year, 'Q') - 1 as end_qtr
from dual;
select (TO_DATE(TRUNC(ADD_MONTHS(sysdate - interval '2' year, -3),'Q'),
'DD-MM-YY')) AS BEG_QTR, TRUNC(ADD_MONTHS(sysdate - interval '2' year, +1), 'Q')
-1 AS END_QTR from DUAL

Number of particular days between two dates

Is there an "easy" way to calculate the count of a particular day between two dates (e.g., say I wanted to know the number of Tuesdays between 1st January, 2000 and today)? Moreover, the same question applies more broadly to different units (e.g., how many 2pms between two dates, how many Februaries, how many 21st Augusts, etc.)... The best I've come up with (for days between dates) is this:
with calendar as (
select to_char(:start + level - 1, 'DAY') dayOfWeek
from dual
connect by level <= ceil(:end - :start)
)
select dayOfWeek, count(dayOfWeek)
from calendar
group by dayOfWeek;
I would have to create a view of this -- hardcoding the start and end dates -- to make it convenient(ish) to use; either that or write a function to do the dirty work. That wouldn't be difficult, but I'm wondering if there's already an Oracle function that could do this, accounting for things like leap days, etc.
EDIT This popped up in the related links when I posted this. That more-or-less answers the question for days and I know there's a months_between function that I could use for particular months. Any other related functions I should know about?
Replace start/end dates with your dates. This query calc number of TUE from Jan 1-2013 till today, which is 18:
SELECT count(*) number_of_tue
FROM
(
SELECT TRUNC(TO_DATE(Sysdate), 'YEAR') + LEVEL-1 start_dt
, To_Char(TRUNC(TO_DATE(Sysdate), 'YEAR') + LEVEL - 1, 'DY') wk_day
, To_Char(TRUNC(TO_DATE(Sysdate), 'YEAR') + LEVEL - 1, 'D') wk_day#
, trunc(Sysdate) end_dt
FROM DUAL
CONNECT BY LEVEL <= trunc(Sysdate) - trunc(Sysdate, 'YEAR') -- replace with your start/end dates
)
WHERE wk_day# = 3 -- OR wk_day = 'TUE' --
/

Timestamps and Intervals: NUMTOYMINTERVAL SYSTDATE CALCULATION SQL QUERY

I am working on a homework problem, I'm close but need some help with a data conversion I think. Or sysdate - start_date calculation
The question is:
Using the EX schema, write a SELECT statement that retrieves the date_id and start_date from the Date_Sample table (format below), followed by a column named Years_and_Months_Since_Start that uses an interval function to retrieve the number of years and months that have elapsed between the start_date and the sysdate. (Your values will vary based on the date you do this lab.) Display only the records with start dates having the month and day equal to Feb 28 (of any year).
DATE_ID START_DATE YEARS_AND_MONTHS_SINCE_START
2 Sunday , February 28, 1999 13-8
4 Monday , February 28, 2005 7-8
5 Tuesday , February 28, 2006 6-8
Our EX schema that refers to this question is simply a Date_Sample Table with two columns:
DATE_ID NUMBER NOT Null
START_DATE DATE
I Have written this code:
SELECT date_id, TO_CHAR(start_date, 'Day, MONTH DD, YYYY') AS start_date ,
NUMTOYMINTERVAL((SYSDATE - start_date), 'YEAR') AS years_and_months_since_start
FROM date_sample
WHERE TO_CHAR(start_date, 'MM/DD') = '02/28';
But my Years and months since start column is not working properly. It's getting very high numbers for years and months when the date calculated is from 1999-ish. ie, it should be 13-8 and I'm getting 5027-2 so I know it's not correct. I used NUMTOYMINTERVAL, which should be correct, but don't think the sysdate-start_date is working. Data Type for start_date is simply date. I tried ROUND but maybe need some help to get it right.
Something is wrong with my calculation and trying to figure out how to get the correct interval there. Not sure if I have provided enough information to everyone but I will let you know if I figure it out before you do.
It's a question from Murach's Oracle and SQL/PL book, chapter 17 if anyone else is trying to learn that chapter. Page 559.
you'll want MONTHS_BETWEEN in that numtoyminterval as the product of subtracting two date variables gives the answer in days which isn't usable to you and the reason its so high is you've told Oracle the answer was in years! Also use the fm modifier on the to_char to prevent excess whitespace.
select date_id,
to_char(start_date, 'fmDay, Month DD, YYYY') as start_date,
extract(year from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
|| '-' ||
extract(month from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
as years_and_months_since_start
from your_table
where to_char(start_date, 'MM/DD') = '02/28';
You can simplify the answer like this
SELECT date_id, start_date, numtoyminterval(months_between(sysdate, start_date), 'month') as "Years and Months Since Start"
FROM date_sample
WHERE EXTRACT (MONTH FROM start_date) = 2 AND EXTRACT (DAY FROM start_date) = 28;

Find how many days from begining of the month to current day (Oracle)

How do I determine how many days in a month, from the first to the current day (could be anywhere into the month). So if I have a field that gives 6/01/12 5:32:13 PM and 6/07/12 5:33:04 PM how do I get the difference?
I think something like this should do it:
SELECT TRUNC(SYSDATE) - TRUNC(SYSDATE,'MONTH') + 1 FROM DUAL
SELECT EXTRACT(DAY FROM sysdate) FROM dual;