Creating a row number column based on another columns value - sql

I have the following data:
it's a calendar that has date and year/week number there week is our internal calendar. what i want to do is sort by the most recent week number and have that be rel_week_index=1, and then the previous week be rel_week_index=2, etc. I got this to work by doing ROW_NUMBER() OVER(PARTITION BY YEAR ORDER BY YEAR DESC), however, once you get to the previous year, this rel_week_index column starts over at 1, which isn't what i want.
Ideally year_week=202111 would be rel_week_index=1, year_week=202110 would be rel_week_index=2 and so on.

If I understood you correctly this is the query to calculate row number based on year and week combination in descending order:
ROW_NUMBER() OVER( ORDER BY YEAR_week DESC)

Related

SQL moving average by weekday with moving range for last 4 weeks, excluding current week

I'm trying to calculate 7 day moving average by weekday, for last 4 weeks but not including the current week. The code below calculates the average however that includes the current week's data. How can I exclude the current week and only calculate last 4 Sundays, Mondays, Tuesdays, etc. Please note, the average will differ by day. For example, each Sunday will have a different average based on the last 4 weeks.
Attached is the sample data and desired results Data example
Select a.Date, a.WeekDay,
avg(a.count) Over(partition by a.WeekDay order by a.Date rows between 3 preceding and current row) as rolling_avg
from
(Select Date, WeekDay, Count from Sales) a
Where a.Date >= current_date- 7*7
You almost have it, change the way to use following ROWS. You need to use FOLLOWING 1 to use starting next row and 4 as the limit. I also changed the order by date desc that, way the next row is the previous week.
Select
a.Date_V,
a.WeekDay,
--AVG(CAST(a.count_v AS DECIMAL(8,2))) OVER /*cast optional*/
AVG(a.count_v) Over
(partition by a.WeekDay order by a.date_v desc
ROWS BETWEEN 1 FOLLOWING AND 4 FOLLOWING) as rolling_avg
FROM
(Select Date_V, WeekDay, Count_v from historic_data) a
Where a.Date_v >= GETDATE()- 7*7
ORDER BY a.date_v desc
I created following SQL Fiddle for testing if someone wants to play with it.

Oracle SQL - only last reading before the 1st of next month

Number field is the key but it can be modified several times a month (or not all all). Need to pull for last 6 complete month/year (Month_Year), only last reading before the 1st of next month.
So you want something like this?
select number, to_char(sysdate, 'Mon-YYYY') as month_year,
max(reading) keep (dense_rank first order by modified_date_time desc) as reading
from t
group by number;
The most recent reading for each number along with the current date formatted as Mon-YYYY.

SQL: Getting the min date of a series of dates partitioning by if previous date is more than 1 day ago

I have a data import which happens every week and when it starts, lasts a couple of days. As a result, in the date column, I have multiple dates for each data import. I would like to get the min date of each import. Is this possible in SQL? Specifically, in Google BigQuery. Example:
date desired_output
4/25/17 4/25/17
4/26/17 4/25/17
4/27/17 4/25/17
5/2/17 5/2/17
5/3/17 5/2/17
5/10/17 5/10/17
5/16/17 5/16/17
5/17/17 5/16/17
5/23/17 5/23/17
5/24/17 5/23/17
5/30/17 5/30/17
5/31/17 5/30/17
6/5/17 6/5/17
6/6/17 6/6/17
You can identify groups of dates that are in order sequentially -- this is a gaps and islands problem. Perhaps this will do what you want:
select date,
min(date) over (partition by date_add(date, interval - seqnum_d day)) as desired_output
from (select t.*,
dense_rank() over (order by date) as seqnum_d
from t
) t
The date arithmetic identifies sequences of dates by subtracting a sequence -- voila! The result is a constant.
Note: This assumes that sequences of dates have gaps.
Also, I used dense_rank() so it can handle multiple entries on a single date.

How to get last and first date of every week from predefined date table (oracle SQL)

I have Table D_date in which all dates of a year, week number,quarter number etc attributes are defined. I just want to get first and last date of every week of year 2015.
Sample D_date tabe attached.
It is simple min/max if I understand you right
SELECT calendar_year_nbr, week, min(actual_date),max(actual_date)
FROM D_date
GROUP BY calendar_year_nbr, week
I just want to get first and last date of every week of year 2015.
Since you have precomputed values already stored in the table, you could directly use MIN and MAX as aggregate functions along with GROUP BY.
For example,
SELECT MIN(actual_date) min_date,
MAX(actual_date) max_date,
calendar_week_nbr
FROM d_date
WHERE calendar_year_nbr = 2015
GROUP BY calendar_week_nbr
ORDER BY min_date;
Another way is to use ROWNUM() OVER() analytic function.

Data for specific date

My report gets data for the 1st of the current month. Let's say the 1st has still not come then how would I make the report show the data for the 1st of the previous month.
Thanks.
Simply use a select top 1 from your table, filtering by extract(day from yourDateColumn) = 1 to get only the rows with the data for the 1st day of any month, and order them in descending order by your date column (order by yourDateColumn desc), so that you always get the 1st day of the last available month in your table.
Docs for Oracle EXTRACT function