Data for specific date - sql

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

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.

Creating a row number column based on another columns value

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)

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.

Selecting sets of data and creating a new column in SQL Server

In SQL Server can you select the first set of values (i.e. week numbers 1 - 52) give them another value in a new column, then select the next lot of values.
The problem I am having is the data table I am working on has week numbers for each financial year, which starts the first Sunday after 1 October. So it simply iterates 1 - 52 for each financial year.
I am trying to make a column in a view that grabs the first 52 gives them the a financial year value of 1, then grabs the next 52 and gives them a financial year value of 2 etc (obviously with year 1 starting at the first record). I do have the Week Ending Date column to work with also.
Here is a snippet of the table:
Is this possible?
Leave the Sundays and Octobers. If I understand correctly, you only need to assign a rank to each occurrence of week number in order of the ending dates.
Please try this (but use copy of the table or transaction to check first; of course T is name of your table):
update T
set fiscal_year = YearNumbers.FiscalYear
from T
inner join
(
select WeekEndingDate, WeekNumber, DENSE_RANK() over (partition by WeekNumber order by WeekEndingDate) as FiscalYear
from T
) as YearNumbers
on T.WeekEndingDate = YearNumbers.WeekEndingDate and T.WeekNumber = YearNumbers.WeekNumber

use of week of year & subsquend in bigquery

I need to show distinct users per week. I have a date-visit column, and a user id, it is a big table with 1 billion rows.
I can change the date column from the CSVs to year,month, day columns. but how do I deduce the week from that in the query.
I can calculate the week from the CSV, but this is a big process step.
I also need to show how many distinct users visit day after day, looking for workaround as there is no date type.
any ideas?
To get the week of year number:
SELECT STRFTIME_UTC_USEC(TIMESTAMP('2015-5-19'), '%W')
20
If you have your date as a timestamp (i.e microseconds since the epoch) you can use the UTC_USEC_TO_DAY/UTC_USEC_TO_WEEK functions. Alternately, if you have an iso-formatted date string (e.g. "2012/03/13 19:00:06 -0700") you can call PARSE_UTC_USEC to turn the string into a timestamp and then use that to get the week or day.
To see an example, try:
SELECT LEFT((format_utc_usec(day)),10) as day, cnt
FROM (
SELECT day, count(*) as cnt
FROM (
SELECT UTC_USEC_TO_DAY(PARSE_UTC_USEC(created_at)) as day
FROM [publicdata:samples.github_timeline])
GROUP BY day
ORDER BY cnt DESC)
To show week, just change UTC_USEC_TO_DAY(...) to UTC_USEC_TO_WEEK(..., 0) (the 0 at the end is to indicate the week starts on Sunday). See the documentation for the above functions at https://developers.google.com/bigquery/docs/query-reference for more information.