I have a table in snowflake that looks like this:
starting_date fiscal_week calendar_week
2020-12-31 53 53
2021-01-01 53 53
2021-01-08 53 01
The fiscal_week and calendar_week look up off of the starting_date (format is YYYYMMDD). The fiscal week should follow the 1-53 week number in the year. The calendar_week does not necessarily need to start off on a Sunday, it just needs to start off on the first day of the year as week 01.
Thus, I would like it to look like this:
starting_date fiscal_week calendar_week
2020-12-31 53 53
2021-01-01 53 01
2021-01-08 53 02
I've tried using the weekiso and week functions such as below but still am not getting the desired results. The results still reflect what is shown in the first table.
select *, weekiso (starting_date) as calendar_week from my table
Is there a specific way to do this in the snowflake database? I'm assuming that's where my issue is coming from but I'm not sure.
I'm new to using snowflake so any help would be appreciated!
You can extract days of the year and do some arithmetic on that. It's probably easier to just change the week of year policy at session level though
select floor((dayofyear(starting_date)+6)/7)
Related
I am new to sql and this is my first ever question. I am working with a sample database that I want to extract specific information from to display as a dashboard. The issue is that I can do this partially but I cannot seem to figure it out properly.
``SELECT
S_date as date,
p_time as time,
process_id as process,
sc_gun as scannumb,
sum(line_qty) as linetotal,
sum(area_qty) as areatotal
FROM dbfile6
WHERE
process_id in('0010','0020','0030')
and sc_gun in = ('10','20','30','40','50')
and s_date = curdate() - 1 and p_time between '22:00:00' and '23:59:59'
or s_date = curdate() and p_time between '00:00:00' and '06:00:00'
GROUP BY p_time, s_date, process_id, sc_gun
ORDER BY s_date, process_id
What do I want to display?
I can do partially where I want it to work to yesterdays date (s_date) recurring but I want this to only happen Monday to Friday, skipping the weekend so when we are on Monday, it looks at Fridays data from the database.
I want to show the time as a range, a night range. The range is 20:00:00 - 06:00:00. The range is tricky as it crosses over to the next day, this could work for Monday to Thursday but not Friday as there is no working weekend so what would I do here? In addition to this, I can sum up the values successfully and display it as averages for each process but then once I add the time in, it displays each result individually.
The table below is what it looks like in the database, however as mentioned earlier, the desired result is for each process to have the line_qty and area_qty summed up by time range and a day and night cycle.
s_date
p_time
process_id
sc_gun
line_qty
area_qty
04/05/2022
04:49:52
0010
10
2
12
03/05/2022
11:50:00
0010
10
5
14
03/05/2022
19:50:00
0010
10
7
16
03/05/2022
13:50:00
0020
20
4
6
03/05/2022
19:50:00
0010
10
7
16
The below gives me week numbers where week 1 starts on 1/4/2021
date_trunc('week', transaction_date) as week_number
How can I create a week_number where the week starts on January 1st and counts up 7 days for every week thereafter (for every year)?
And round up/down to 52 weeks at the end of the year?
Code attempted:
This doesn't give me the answer, but I'm thinking something like this might work...
ceil(extract(day from transaction_date)/7) as week_number
Expected Output:
transaction_date
week_number
1/1/2020
1
1/8/2020
2
...
...
12/31/2020
52
1/1/2021
1
1/8/2021
2
...
...
12/27/2021
52
12/28/2021
52
12/29/2021
52
12/30/2021
52
12/31/2021
52
1/1/2022
1
Thanks in advance!
A simple way is to use date arithmetic:
select 1 + (transaction_date - date_trunc('year', transaction_date)) / 7 as year_week
The below gives me week numbers where week 1 starts on 1/4/2021
It is the default behaviour and it is defined that way in ISO.
WEEK_OF_YEAR_POLICY
Type Session — Can be set for Account » User » Session
Description
Specifies how the weeks in a given year are computed.
Values
0: The semantics used are equivalent to the ISO semantics, in which a week belongs to a given year if at least 4 days of that week are in that year.
1: January 1 is included in the first week of the year and December 31 is included in the last week of the year.
Default 0 (i.e. ISO-like behavior)
It could be overrriden on multiple levels. The most granular is on the session level:
ALTER SESSION SET WEEK_OF_YEAR_POLICY = 1;
Then you could use the standard code:
SELECT date_trunc('week', transaction_date) as week_number
FROM ...;
I need to have date sorting with the partial dates. I have a table with the following columns.
Day Month Year
-- ---- -----
NULL 03 1990
26 10 1856
03 07 Null
31 NULL 2018
NULL NULL NULL
I have a grid in which One of the column is Date where I am combining the above three columns and displays the dates.
Now I want sorting on this date column in the grid. The sort order of the dates should be like following :
[blank date]
22 [day]
March
April 12
May
July 29
August
September
September 14
October
1948
October 1948
October 1 1948
July 1976
1977
July 1977
July 23 1977
December 1981
December 29 1981
I have tried various ways to achieve this. But I am not able to get the desired result. Following are some of the ways I have applied.
I have tried sorting by creating the stored procedure in which I am creating the whole date by combining 3 columns and converting them in standard date formats and comparing the values. I have also tried by creating the computed property in the model and sorting them accordingly.
How can I do this in SQL?
I think you could do:
order by coalesce(year, '0000'), coalesce(month, '00'), coalesce(day, '')
You can be more explicit, but this puts the NULL values before the other values in the column.
Note: This uses the SQL standard operator for string concatenation. Not all databases support this, so you might need to tweak the code for your database.
I'm trying to modify a query to extract day, week, month, etc from a date where the field in "extract(field from datetime)" is provided by query values.
This is for Redshift. The simplified example I'm working on is below. Trying to create a summary for various date buckets without repeating the same query over and over.
SELECT
visit_start_date,
period_type,
extract(period_type from visit_start_date) as Period
FROM f_visit_dates
cross join d_period_type
Input table named f_visit_dates
with single date column visit_start_date and two rows:
visit_start_date
----------------
'2017-03-14'
'2018-05-06'
Input table named d_period_type contains five rows and one varchar(10) column:
period_type
-----------
'day'
'week'
'month'
'quarter'
'year'
Output should look like the following:
visit_start period_type period
----------- ----------- ------
2017-03-14 day 73
2017-03-14 week 11
2017-03-14 month 3
2017-03-14 quarter 1
2017-03-14 year 2017
2018-05-06 day 126
2018-05-06 week 18
2018-05-06 month 5
2018-05-06 quarter 2
2018-05-06 year 2018
I should add that the above as written doesn't work, obviously. I've also tried supplying the value through a "WITH" clause but that also doesn't work.
I would like a column displaying the fiscal week. Our fiscal year begins in April.
So far I have the below, using datename(ww,DateAndTime) as Week
DateAndTime Week
2015-04-01 22:45 14
2015-06-14 13:22 25
2015-12-02 09:15 49
2016-01-01 07:35 1
I would like the output to show:
DateAndTime Week Fiscal Week
2015-04-01 22:45 14 1
2015-06-14 13:22 25 12
2015-12-02 09:15 49 36
2016-01-01 07:35 1 41
While I don't understand the logic behind the fiscal week (the difference between 1 and 41 is 40, but between 14 and 1 it's 39), maybe I'm missing something or you made a typo.
However, in general you'd do something like this (assuming the difference is 40 weeks):
SELECT week, (week+40)%52 AS fw FROM ...
If the fiscal year starts at a different week every each (say, 13th or 14th week depending on year), you can use the date and time functions, but they may vary between SQL versions. In MySQL you have YEAR(), MONTH(), WEEK(), etc.
For example:
SELECT week, (week+(52-WEEK(CONCAT_WS('-', YEAR(NOW()), '04-01'))))%52 FROM ...
But it might be overkill.
Note: It is possible to count the other way: if you subtract the diff from the week instead of adding, you will need to add 52 if the number is negative. You can do that by adding 52 and then doing modulo (%) 52.