Values on the frist day of the week - snowflake-cloud-data-platform

I have a table in snowflake like below
StockName Date Value
GOOG 10/12/2021 $100
GOOG 10/11/2021 $995
Now i want to create a new column which would give me results that would show the value of the stock on weeks first day which would be monday or the next day if monday is a holiday
StockName date Value value_weekstart
GOOG 10/12/2021 $1000 $995
GOOG 10/11/2021 $995 $995

The following should work:
SELECT
stockname,
date,
value,
FIRST_VALUE(value) OVER (PARTITON BY stockname, datetrunc('week',date) ORDER BY dayofweekiso(date)) as value_weekstart
FROM table_name
ORDER BY 1,2;
that will return the "sunday price" if there is one, but will side step the possible jump if you use the stock dayofweek and someone changes the setting on your instance.
If you really want sunday to be last going to (dayofweekiso(date)+6)%7 will roll it around to last position.

Related

How to get a weekly view from date field in snowflake

I have a small query mentioned below.
Need to break down a date field "DAY" to monthly and weekly wise in snowflake.
Input:
DAY
-------
2022-06-09
2022-04-04
Output
DAY_MONTH
----------
2022-06-01
2022-04-01 Monthly wise--- Its done
Here I have used
DATE_FROM_PARTS( YEAR(DAY), MONTH(DAY), 1) AS DAY_MONTH
DAY_WEEK
----------
2022-06-06
2022-04-04
They should be first day of working days like (Monday). How to do that for a weekly view?
I think you're looking for the date_trunc function:
set ts = '2022-07-07 11:14:00'::timestamp;
select date_trunc('DAY', $TS);
select date_trunc('WEEK', $TS);
select date_trunc('MONTH', $TS);
This is showing for a timestamp to show how it truncates to the day, but it works the same way for date types. Truncating to the week will start by the week_start parameter that's in effect (it will default to Monday as the start for this function):
https://docs.snowflake.com/en/sql-reference/parameters.html

Group by dateTime (Set from till)

I am trying to create a group by date function. But i don't want to get results from 00:00 till 23:59, i want to get results from 6:00 till 5:59.
Can i change date from till statement?
Example
Select * from Sales group by Convert(date,dateOfSell)
//set statement from 6:00 till next day 5:59
First substract 6 hours from the datetime, and then cast it do a date. Then you can group on this new date.
e.g.
SELECT CAST(DATEADD(hour,-6,dateOfSell) AS DATE),MIN(dateOfSell),MAX(dateOfSell),COUNT(*)
FROM Sales
GROUP BY CAST(DATEADD(hour,-6,dateOfSell) AS DATE)

Create a rolled up week column using the day column and populate the value with first monday of the week: Snowflake database

Snowflake Database:
I have a day level table , i am trying use the day column to create a week column with the value of first Monday's of the week as a week value using following function. even though the table have a data for all seven days ( Monday through Sunday ) of the week. The following function will create a week column with five working days ( Monday through Friday) only rolled up to a week leaving Saturday and Sunday. is there any function which i can use to grab all seven days of data under one week in SNOWFLAKE DATABASE (first monday of the week).
CURRENT FUNCTION USED:
select dateadd('day', (extract('dayofweek_iso', current_date()) * -1) +1 , current_date() );
Snowflake provides DATE_TRUNC() to roll up date to corresponding year/month/week/day.
For the mentioned use case following query will roll up to Monday of the week.
select date_trunc('WEEK',current_date()) ;
Snowflake last_day() function can be used to achieve that. You can easily get the last day of week (Sunday) and subtract 6 days to get the first day of that week (Monday) like that:
select last_day(current_date(), 'week')-6 as first_day_of_week;

Oracle: select max date per month per year from table

I have a table with a set of business dates I need to select the max date per month and year tried using the last_day function but that returns the last day not the max date of that month.please help me out.
MAX is an aggregate function, so you need to figure out how to group all of the days of the month together. The easiest way to do that is apply a function that will return the same value for every day in that month. LAST_DAY would work, but I prefer TRUNC (with 'MM' specified).
SELECT MAX(your_column) FROM your_table GROUP BY TRUNC(your_column, 'MM')

Business Week Group in SQL (instead of calendar week)

My requirement is that I want to find business-week-ending (not the calender week) given a DATE column from the sales table in MSSQL.
Using different techniques I was able to find the [Calender] week-endings (and week-starting) dates corresponding to DATE in the table.
Since our business week ends on Wednesday [DOW 3 or 4 depending when the week started], I tried to deduct number of days from the week ending dates to pull it back to Wednesday. The idea did work pretty good with a flaw. Works fine as long as the Date in the table is greater than DOW 3 or 4. Any suggestion?
SELECT DateAdd(wk, DateDiff(wk, 0, Recons_Sales_Details.Recons_Date), 0) + 2
You need to look into SET DATEFIRST to do this:
SET DATEFIRST 4 --4 is Thursday week start
SQL Fiddle Demo

Resources