Impala - Grouping By Week - sql

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 -

Related

SQL Presto - Week function to star on a Sunday

I'm trying to extract the week number from a date, and I want the week to be counted from Sunday to Saturday. This is what I currently have, but I can't seem to find any solution for this is SQL Presto.
SELECT WEEK(date) AS weeknum
Can this be solved?
Thank you!
One method is:
select week(date + interval '1 day') - interval '1 day'
Note: This may not work on the last day of the year.
Alternatively you can use the MySQL-like functions:
select date_format(date, '%V')
This has the week starting on Sunday.

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

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!!

PLSQL - How to find Monday and Friday of the week of a given date

I have spent days trying to figure this out to no avail, so hopefully someone can help me. I have a queried date set which contains several fields including a column of dates. What I want to do is create a new field in my query that tells what the Monday and Friday is for the week of that row's particular date.
So for example; if the date in one of my rows is "1/16/18",
the new field should indicate "1/15/18 - 1/19/18".
So basically I need to be able to extract the Monday date (1/15/18) and the Friday date (1/19/18) of the week of 1/16/18 and then concatenate the two with a dash ( - ) in between. I need to do this for every row.
How on earth do I do this? I've been struggling just to figure out how to find the Monday or Friday of the given date...
Assuming that your column is of type date, you can use trunc to get the first day of the week (monday) and then add 4 days to get the friday.
For example:
with yourTable(d) as (select sysdate from dual)
select trunc(d, 'iw'), trunc(d, 'iw') + 4
from yourTable
To format the date as a string in the needed format, you can use to_char; for example:
with yourTable(d) as (select sysdate from dual)
select to_char(trunc(d, 'iw'), 'dd/mm/yy') ||'-'|| to_char(trunc(d, 'iw') + 4, 'dd/mm/yy')
from yourTable
gives
15/01/2018-19/01/18
There may be a simpler, canonical Oracle method to this but you can still reduce it to a simple calculation on your own either way. I'm going to assume you're dealing with only dates falling Monday through Friday. If you do need to deal with weekend dates then you might have to be more explicit about which logical week they should be attached to.
<date> - (to_char(<date>, 'D') - 2) -- Monday
<date> + (6 - to_char(<date>, 'D')) -- Friday
In principle all you need to do is add/subtract the appropriate number of days based on the current day of week (from 1 - 7). There are some implicit casts going on in there and it would probably be wise to handle those better. You might also want to check into NLS settings to make sure you can rely on to_char() using Sunday as the first day of week.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
You can also use the NEXT_DAY function, as in:
SELECT TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '7' DAY AS PREV_MONDAY,
TRUNC(NEXT_DAY(SYSDATE, 'FRI')) AS NEXT_FRIDAY
FROM DUAL;
Note that using the above, on weekends the Monday will be the Monday preceding the current date, and the Friday will be the Friday following the current date, i.e. there will be 11 days between the two days.
You can also use
SELECT TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '7' DAY AS PREV_MONDAY,
TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '3' DAY AS NEXT_FRIDAY
FROM DUAL;
in which case the Monday and Friday will always be from the same week, but if SYSDATE is on a weekend the Monday and Friday returned will be from the PREVIOUS week.

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

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;