I separated the time stamp to year and quarter and week using this query
select EVENTTIMESTAMP, Year (EVENTTIMESTAMP), QUARTER (EVENTTIMESTAMP), Week (EVENTTIMESTAMP) from KAP.COGNOS
there only two things i was trying to enhance in it
1- the weeks returns a number between 1-54, I want it to be return 13 or 14 weeks as weeks of Quarters.
2- to add word before the number of Quarter 'Quarter' and then it's number, and so to the weeks to show word 'Week' then the number
Many Thanks!!
Related
Currently have rows aggregated by week number.
SELECT to_char(date, 'IYYY-MM-IW') AS week, from TABLE GROUP BY week
The results will show the form "2021-07-29". Is it possible to change the week number such that it is the number week of the month (instead of year).
For example, instead of "2021-07-29", we convert to "2021-07-04" since the 29th week of the year is actually the 4th week of the month.
Quote from the manual
W week of month (1–5) (the first week starts on the first day of the month)
So you can use:
to_char(date, 'YYYY-MM-W')
For e.g 2021-10-18 this yields 2021-10-3 (third week in October)
I have 12 monthly tables, one for each month of the year 2019, records are order by an identifier (mmsi) and datetime (timestamp). I have calculated the interval (linetime2) between two consecutive rows. see below (August 2019):
Now I need to convert interval in hours in a new column. How can I do it? Can I run this:
SELECT EXTRACT(epoch FROM linetime2)/3600
or I have to take into account the number of days in a month and year?
In postgresql extract(week from '2014-12-30'::timestamp) gives week number 1 of 2015. How do I extract the associated year that corresponds to the week number? Using extract(year ... gives 2014
Since week is the ISO-defined week, you want the isoyear:
#= select extract(isoyear from '2014-12-30'::timestamp);
date_part
-----------
2015
(1 row)
From the docs:
By definition, ISO weeks start on Mondays and the first week of a year
contains January 4 of that year. In other words, the first Thursday of
a year is in week 1 of that year.
In the ISO week-numbering system, it is possible for early-January
dates to be part of the 52nd or 53rd week of the previous year, and
for late-December dates to be part of the first week of the next year.
For example, 2005-01-01 is part of the 53rd week of year 2004, and
2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is
part of the first week of 2013. It's recommended to use the isoyear
field together with week to get consistent results.
According to the postgresql documentation for extract week:
The number of the week of the year that the day is in. By definition
(ISO 8601), the first week of a year contains January 4 of that year.
(The ISO-8601 week starts on Monday.) In other words, the first
Thursday of a year is in week 1 of that year. (for timestamp values
only)
Based on this, you might have to do a little logic:
case when extract(week from '2014-12-30'::timestamp) = 1
and extract(month from '2014-12-30'::timestamp) = 12
then extract(year from '2014-12-30'::timestamp)+1
else extract(year from '2014-12-30'::timestamp)
end
I'm trying to separate weeks from timestamp per quarter so it should be between 1-13 week per quarter so I used function week() but it takes between 1-52 week as whole year so I made it to be divided by function of quarter like below
select Week (EVENTTIMESTAMP) / QUARTER (EVENTTIMESTAMP) from KAP
The thing here that results aren't accurate; for example it shows:
time stamp 2014-07-06 12:13:03.018
week number 9
which isn't correct because July is first month in Q3 and it's in the 6 days so it should be 1 week from Q3 not 9.
Any suggestion where it go wrong?
You want something like WEEK modulo 13 to get week number within a quarter. You will have to tinker with 'modulo 13 yields 0..12' by adding or subtracting one at appropriate points.
Some minimal Google searching using 'ibm db2 sql modulo' yields DB2 MOD function:
The MOD function divides the first argument by the second argument and returns the remainder.
Hence MOD(WEEK(...), 13), except you probably need MOD(WEEK(...)-1, 13) + 1, as intimated already.
You may need to watch for what the WEEK() function does at year ends:
The WEEK function returns an integer in the range of 1 to 54 that represents the week of the year. The week starts with Sunday, and January 1 is always in the first week.
I'm curious about how they can come up with week 54. I suppose it requires 1st January to be a Saturday (so 2nd January is the start of week 2) of a leap year, as in 2000 and 2028. Note that week 53 and (occasionally) week 54 will show up as weeks 1 and 2 of Q5 unless you do something. Also, Saturday 2000-03-25 would be the end of Q1 and Sunday 2000-03-26 would be the start of Q2 under the regime imposed by the WEEK() function and a simple MOD(WEEK(...), 13) calculation. You're likely to have to tune this to meet your real requirements.
There's also the WEEK_ISO() function:
The WEEK_ISO function returns an integer in the range of 1 to 53 that represents the week of the year. The week starts with Monday and includes seven days. Week 1 is the first week of the year that contains a Thursday, which is equivalent to the first week that contains January 4.
Note that under the ISO scheme, the 3rd of January can be in week 52 or 53 of the previous year, and the 29th of December can be in week 1 of the next year. Curiously, there doesn't seem to be a YEAR_ISO() function to resolve such ambiguities.
In a data warehouse, the proper solution to this is to create a time dimension that contains static mappings for days/weeks/months/quarters/years. This provides the ability to define these based on your business' fiscal calendar (if it is not following on the calendar year).
See: http://www.kimballgroup.com/1997/07/10/its-time-for-time/ for more information.
I have a small application built in Excel. It is a time management workbook where I record the hours worked on projects for each month.
In one column I have the possible days
e.g.
DAY 1
DAY 2
DAY 3
DAY 4
....
DAY 22
for the possible 22 workdays in a month. I have a worksheet for each month.
For each of those day numbers how would I calculate the date for that day in the Month?
You can try WORKDAY - Does the following helps?