How to Create Iso-8601 Gregorian Date Table in Postgres

how to create ISO-8601 gregorian date table in postgres

see the below example

SELECT mydate calendar_date
,EXTRACT(WEEK FROM mydate) week_num
,EXTRACT(month FROM mydate) month_num
,to_char(mydate,'Mon') month_name
,EXTRACT(Quarter FROM mydate) quarter_num
,EXTRACT(year FROM mydate) calendar_year
,EXTRACT(DOW FROM mydate) iso_dayofweek
,to_char(mydate, 'day') dayofweek_name
FROM (
SELECT now()::DATE mydate
) t

Result:

calendar_date week_num month_num month_name quarter_num calendar_year iso_dayofweek dayofweek_name 
------------- -------- --------- ---------- ----------- ------------- ------------- --------------
2015/04/24 17 4 Apr 2 2015 5 friday

You can use generate_series() to get all date in a year for ex: 2015

select generate_series(0,364) + date'1/1/2015'

this will produce date from 1/1/2015 - 31/12/2015, and use this select instead of SELECT now()::DATE in the given example

If you want to create table for year 2015 then you can use the following query

CREATE TABLE mycal_2015 AS
SELECT row_number() OVER () date_key
,mydate calendar_date
,EXTRACT(WEEK FROM mydate) week_num
,EXTRACT(month FROM mydate) month_num
,to_char(mydate,'Mon') month_name
,EXTRACT(Quarter FROM mydate) quarter_num
,EXTRACT(year FROM mydate) calendar_year
,EXTRACT(DOW FROM mydate) iso_dayofweek
,to_char(mydate, 'day') dayofweek_name
FROM (
SELECT generate_series(0, 364) + DATE '1/1/2015' mydate
) t

and the table will look like select * from mycal_2015

date_key calendar_date week_num month_num month_name quarter_num calendar_year iso_dayofweek dayofweek_name 
-------- ------------- -------- --------- ---------- ----------- ------------- ------------- --------------
1 2015/01/01 1 1 Jan 1 2015 4 thursday
2 2015/01/02 1 1 Jan 1 2015 5 friday
3 2015/01/03 1 1 Jan 1 2015 6 saturday
4 2015/01/04 1 1 Jan 1 2015 0 sunday
5 2015/01/05 2 1 Jan 1 2015 1 monday
6 2015/01/06 2 1 Jan 1 2015 2 tuesday
...
.
.
.
364 2015/12/30 53 12 Dec 4 2015 3 wednesday
365 2015/12/31 53 12 Dec 4 2015 4 thursday

POSTGRESQL: EXTRACT FUNCTION

The PostgreSQL extract function extracts parts from a date

Syntax : extract( unit from date )

date is a date, timestamp, time, or interval value from which the date
part is to be extracted.

unit is the unit type of the interval such as day, month, minute,
hour, and so on

It can be one of the following:

unit            description                                                                                                                   
--------------- -----------------------------------------------------------------------------------------------------------------------------
century Uses the Gregorian calendar where the first century starts at '0001-01-01 00:00:00 AD'
day Day of the month (1 to 31)
decade Year divided by 10
dow Day of the week (0=Sunday, 1=Monday, 2=Tuesday, ... 6=Saturday)
doy Day of the year (1=first day of year, 365/366=last day of the year, depending if it is a leap year)
epoch Number of seconds since '1970-01-01 00:00:00 UTC', if date value. Number of seconds in an interval, if interval value
hour Hour (0 to 23)
isodow Day of the week (1=Monday, 2=Tuesday, 3=Wednesday, ... 7=Sunday)
isoyear ISO 8601 year value (where the year begins on the Monday of the week that contains January 4th)
microseconds Seconds (and fractional seconds) multiplied by 1,000,000
millennium Millennium value
milliseconds Seconds (and fractional seconds) multiplied by 1,000
minute Minute (0 to 59)
month Number for the month (1 to 12), if date value. Number of months (0 to 11), if interval value
quarter Quarter (1 to 4)
second Seconds (and fractional seconds)
timezone Time zone offset from UTC, expressed in seconds
timezone_hour Hour portion of the time zone offset from UTC
timezone_minute Minute portion of the time zone offset from UTC
week Number of the week of the year based on ISO 8601 (where the year begins on the Monday of the week that contains January 4th)
year Year as 4-digits

Note: Extract function is applies to PostgreSQL version 8.4 and above

Date/Time Functions and Operators
generate_series()

Getting a weird result for ISO Date in Postgresql

You should not mix week with year in the extract function as year is for the Gregorian calendar rather than the special ISO calendar.

See section 9.9.1 and comments about week.

to_number(to_char(newdate, 'IW'), '99') is effectively extract(week from newdate)

Changing the yr column to be extract(isoyear from newdate) solves your problem.

Adjusted SQL Fiddle

Postgresql ISOYEAR first date

Ask it to convert the first "ISO day" of the "ISO year":

=> SELECT to_date('2011-0001', 'IYYY-IDDD');
to_date
------------
2011-01-03

How to use date format in postgresql?

You are ordering by the textual representation of the month, here:

order by to_char(order_date, 'Mon') asc 

The result of to_char is a string, so the ordering is alphabetical; Postgres no longer knows this has anything to do with dates, so has no reason to put "jan" before "apr".

You want to order by either the date itself:

order by order_date asc

Or by a numeric representation of the month, which you could get using extract():

order by extract(month from order_date) asc

Note that since your DISTINCT is currently grouping based on the textual representation, you may have trouble making the SELECT and ORDER BY use different representations. This will be easier with an explicit GROUP BY, which can list both representations:

group by extract(month from order_date), to_char(order_date, 'Mon')

Since you can be sure that a given order_date can't produce a separate value for those two columns, this won't change the number of rows, but allows you to write this:

select to_char(order_date, 'Mon')
from meta.ship_error
group by extract(month from order_date), to_char(order_date, 'Mon')
order by extract(month from order_date);

How do you make a timestamp from separate date and time columns in Postgresql?

Just cast the timestamp to a date and add the time (after casting that to a time):

select date_col::date + time_col::time as timestamp_col
from the_table;

hour from timestamp in postgresql for dynamic queries

The timestamp keyword is not required:

select extract(hour from emp_empdt) as h1 
from employee emp

The expression timestamp '2015-05-02 20:05:12' is an ANSI SQL timestamp literal and the keyword timestamp is only required for a literal like that. This is necessary to distinguish such an expression from a character literal ('2015-05-02 20:05:12').

How to get the count of current month Sunday's in psql?

The total number of Sundays for a given date can only be either 0 or 1.

But if you want the number of Sundays within a given date range, then your best bet is a calendar table. To find how many Sundays are in February this year, I'd just

select count(*) 
from calendar
where cal_date between '2011-02-01' and '2011-02-28' and
day_of_week = 'Sun';

or

select count(*)
from calendar
where year_of_date = 2011 and
month_of_year = 2 and
day_of_week = 'Sun';

Here's a basic calendar table that you can start with. I also included a PostgreSQL function to populate the calendar table. I haven't tested this in 8.3, but I'm pretty sure I'm not using any features that 8.3 doesn't support.

Note that the "dow" parts assume your days are in English. But you can easily edit those parts to match any language. (I think. But I could be wrong about "easily".)

-- Table: calendar

-- DROP TABLE calendar;

CREATE TABLE calendar
(
cal_date date NOT NULL,
year_of_date integer NOT NULL,
month_of_year integer NOT NULL,
day_of_month integer NOT NULL,
day_of_week character(3) NOT NULL,
CONSTRAINT calendar_pkey PRIMARY KEY (cal_date),
CONSTRAINT calendar_check CHECK (year_of_date::double precision = date_part('year'::text, cal_date)),
CONSTRAINT calendar_check1 CHECK (month_of_year::double precision = date_part('month'::text, cal_date)),
CONSTRAINT calendar_check2 CHECK (day_of_month::double precision = date_part('day'::text, cal_date)),
CONSTRAINT calendar_check3 CHECK (day_of_week::text =
CASE
WHEN date_part('dow'::text, cal_date) = 0::double precision THEN 'Sun'::text
WHEN date_part('dow'::text, cal_date) = 1::double precision THEN 'Mon'::text
WHEN date_part('dow'::text, cal_date) = 2::double precision THEN 'Tue'::text
WHEN date_part('dow'::text, cal_date) = 3::double precision THEN 'Wed'::text
WHEN date_part('dow'::text, cal_date) = 4::double precision THEN 'Thu'::text
WHEN date_part('dow'::text, cal_date) = 5::double precision THEN 'Fri'::text
WHEN date_part('dow'::text, cal_date) = 6::double precision THEN 'Sat'::text
ELSE NULL::text
END)
)
WITH (
OIDS=FALSE
);
ALTER TABLE calendar OWNER TO postgres;

-- Index: calendar_day_of_month

-- DROP INDEX calendar_day_of_month;

CREATE INDEX calendar_day_of_month
ON calendar
USING btree
(day_of_month);

-- Index: calendar_day_of_week

-- DROP INDEX calendar_day_of_week;

CREATE INDEX calendar_day_of_week
ON calendar
USING btree
(day_of_week);

-- Index: calendar_month_of_year

-- DROP INDEX calendar_month_of_year;

CREATE INDEX calendar_month_of_year
ON calendar
USING btree
(month_of_year);

-- Index: calendar_year_of_date

-- DROP INDEX calendar_year_of_date;

CREATE INDEX calendar_year_of_date
ON calendar
USING btree
(year_of_date);

And a rudimentary function to populate the table. I haven't tested this in 8.3 either.

-- Function: insert_range_into_calendar(date, date)

-- DROP FUNCTION insert_range_into_calendar(date, date);

CREATE OR REPLACE FUNCTION insert_range_into_calendar(from_date date, to_date date)
RETURNS void AS
$BODY$

DECLARE
this_date date := from_date;
BEGIN

while (this_date <= to_date) LOOP
INSERT INTO calendar (cal_date, year_of_date, month_of_year, day_of_month, day_of_week)
VALUES (this_date, extract(year from this_date), extract(month from this_date), extract(day from this_date),
case when extract(dow from this_date) = 0 then 'Sun'
when extract(dow from this_date) = 1 then 'Mon'
when extract(dow from this_date) = 2 then 'Tue'
when extract(dow from this_date) = 3 then 'Wed'
when extract(dow from this_date) = 4 then 'Thu'
when extract(dow from this_date) = 5 then 'Fri'
when extract(dow from this_date) = 6 then 'Sat'
end);
this_date = this_date + interval '1 day';
end loop;

END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

How to create a Calendar table for 100 years in Sql

Here is a generic script you can use in SQL server. just amend the start and end dates:

IF EXISTS (SELECT * FROM information_schema.tables WHERE Table_Name = 'Calendar' AND Table_Type = 'BASE TABLE')
BEGIN
DROP TABLE [Calendar]
END

CREATE TABLE [Calendar]
(
[CalendarDate] DATETIME
)

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = GETDATE()
SET @EndDate = DATEADD(d, 365, @StartDate)

WHILE @StartDate <= @EndDate
BEGIN
INSERT INTO [Calendar]
(
CalendarDate
)
SELECT
@StartDate

SET @StartDate = DATEADD(dd, 1, @StartDate)
END

If you want a more advanced calendar here is one I found on the net a while ago:

CREATE SCHEMA Auxiliary
-- We put our auxiliary tables and stuff in a separate schema
-- One of the great new things in SQL Server 2005
go

CREATE FUNCTION Auxiliary.Computus
-- Computus (Latin for computation) is the calculation of the date of
-- Easter in the Christian calendar
-- http://en.wikipedia.org/wiki/Computus
-- I'm using the Meeus/Jones/Butcher Gregorian algorithm
(
@Y INT -- The year we are calculating easter sunday for
)
RETURNS DATETIME
AS
BEGIN
DECLARE
@a INT,
@b INT,
@c INT,
@d INT,
@e INT,
@f INT,
@g INT,
@h INT,
@i INT,
@k INT,
@L INT,
@m INT

SET @a = @Y % 19
SET @b = @Y / 100
SET @c = @Y % 100
SET @d = @b / 4
SET @e = @b % 4
SET @f = (@b + 8) / 25
SET @g = (@b - @f + 1) / 3
SET @h = (19 * @a + @b - @d - @g + 15) % 30
SET @i = @c / 4
SET @k = @c % 4
SET @L = (32 + 2 * @e + 2 * @i - @h - @k) % 7
SET @m = (@a + 11 * @h + 22 * @L) / 451
RETURN(DATEADD(month, ((@h + @L - 7 * @m + 114) / 31)-1, cast(cast(@Y AS VARCHAR) AS Datetime)) + ((@h + @L - 7 * @m + 114) % 31))
END
GO

CREATE TABLE [Auxiliary].[Calendar] (
-- This is the calendar table
[Date] datetime NOT NULL,
[Year] int NOT NULL,
[Quarter] int NOT NULL,
[Month] int NOT NULL,
[Week] int NOT NULL,
[Day] int NOT NULL,
[DayOfYear] int NOT NULL,
[Weekday] int NOT NULL,
[Fiscal_Year] int NOT NULL,
[Fiscal_Quarter] int NOT NULL,
[Fiscal_Month] int NOT NULL,
[KindOfDay] varchar(10) NOT NULL,
[Description] varchar(50) NULL,
PRIMARY KEY CLUSTERED ([Date])
)
GO

ALTER TABLE [Auxiliary].[Calendar]
-- In Celkoish style I'm manic about constraints (Never use em ;-))
-- http://www.celko.com/

ADD CONSTRAINT [Calendar_ck] CHECK ( ([Year] > 1900)
AND ([Quarter] BETWEEN 1 AND 4)
AND ([Month] BETWEEN 1 AND 12)
AND ([Week] BETWEEN 1 AND 53)
AND ([Day] BETWEEN 1 AND 31)
AND ([DayOfYear] BETWEEN 1 AND 366)
AND ([Weekday] BETWEEN 1 AND 7)
AND ([Fiscal_Year] > 1900)
AND ([Fiscal_Quarter] BETWEEN 1 AND 4)
AND ([Fiscal_Month] BETWEEN 1 AND 12)
AND ([KindOfDay] IN ('HOLIDAY', 'SATURDAY', 'SUNDAY', 'BANKDAY')))
GO

SET DATEFIRST 1;
-- I want my table to contain datedata acording to ISO 8601
-- http://en.wikipedia.org/wiki/ISO_8601
-- thus first day of a week is monday
WITH Dates(Date)
-- A recursive CTE that produce all dates between 1999 and 2020-12-31
AS
(
SELECT cast('1999' AS DateTime) Date -- SQL Server supports the ISO 8601 format so this is an unambigious shortcut for 1999-01-01
UNION ALL -- http://msdn2.microsoft.com/en-us/library/ms190977.aspx
SELECT (Date + 1) AS Date
FROM Dates
WHERE
Date < cast('2021' AS DateTime) -1
),

DatesAndThursdayInWeek(Date, Thursday)
-- The weeks can be found by counting the thursdays in a year so we find
-- the thursday in the week for a particular date
AS
(
SELECT
Date,
CASE DATEPART(weekday,Date)
WHEN 1 THEN Date + 3
WHEN 2 THEN Date + 2
WHEN 3 THEN Date + 1
WHEN 4 THEN Date
WHEN 5 THEN Date - 1
WHEN 6 THEN Date - 2
WHEN 7 THEN Date - 3
END AS Thursday
FROM Dates
),

Weeks(Week, Thursday)
-- Now we produce the weeknumers for the thursdays
-- ROW_NUMBER is new to SQL Server 2005
AS
(
SELECT ROW_NUMBER() OVER(partition by year(Date) order by Date) Week, Thursday
FROM DatesAndThursdayInWeek
WHERE DATEPART(weekday,Date) = 4
)
INSERT INTO Auxiliary.Calendar
SELECT
d.Date,
YEAR(d.Date) AS Year,
DATEPART(Quarter, d.Date) AS Quarter,
MONTH(d.Date) AS Month,
w.Week,
DAY(d.Date) AS Day,
DATEPART(DayOfYear, d.Date) AS DayOfYear,
DATEPART(Weekday, d.Date) AS Weekday,

-- Fiscal year may be different to the actual year in Norway the are the same
-- http://en.wikipedia.org/wiki/Fiscal_year
YEAR(d.Date) AS Fiscal_Year,
DATEPART(Quarter, d.Date) AS Fiscal_Quarter,
MONTH(d.Date) AS Fiscal_Month,

CASE
-- Holidays in Norway
-- For other countries and states: Wikipedia - List of holidays by country
-- http://en.wikipedia.org/wiki/List_of_holidays_by_country
WHEN (DATEPART(DayOfYear, d.Date) = 1) -- New Year's Day
OR (d.Date = Auxiliary.Computus(YEAR(Date))-7) -- Palm Sunday
OR (d.Date = Auxiliary.Computus(YEAR(Date))-3) -- Maundy Thursday
OR (d.Date = Auxiliary.Computus(YEAR(Date))-2) -- Good Friday
OR (d.Date = Auxiliary.Computus(YEAR(Date))) -- Easter Sunday
OR (d.Date = Auxiliary.Computus(YEAR(Date))+39) -- Ascension Day
OR (d.Date = Auxiliary.Computus(YEAR(Date))+49) -- Pentecost
OR (d.Date = Auxiliary.Computus(YEAR(Date))+50) -- Whitmonday
OR (MONTH(d.Date) = 5 AND DAY(d.Date) = 1) -- Labour day
OR (MONTH(d.Date) = 5 AND DAY(d.Date) = 17) -- Constitution day
OR (MONTH(d.Date) = 12 AND DAY(d.Date) = 25) -- Cristmas day
OR (MONTH(d.Date) = 12 AND DAY(d.Date) = 26) -- Boxing day
THEN 'HOLIDAY'
WHEN DATEPART(Weekday, d.Date) = 6 THEN 'SATURDAY'
WHEN DATEPART(Weekday, d.Date) = 7 THEN 'SUNDAY'
ELSE 'BANKDAY'
END KindOfDay,
CASE
-- Description of holidays in Norway
WHEN (DATEPART(DayOfYear, d.Date) = 1) THEN 'New Year''s Day'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))-7) THEN 'Palm Sunday'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))-3) THEN 'Maundy Thursday'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))-2) THEN 'Good Friday'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))) THEN 'Easter Sunday'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))+39) THEN 'Ascension Day'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))+49) THEN 'Pentecost'
WHEN (d.Date = Auxiliary.Computus(YEAR(Date))+50) THEN 'Whitmonday'
WHEN (MONTH(d.Date) = 5 AND DAY(d.Date) = 1) THEN 'Labour day'
WHEN (MONTH(d.Date) = 5 AND DAY(d.Date) = 17) THEN 'Constitution day'
WHEN (MONTH(d.Date) = 12 AND DAY(d.Date) = 25) THEN 'Cristmas day'
WHEN (MONTH(d.Date) = 12 AND DAY(d.Date) = 26) THEN 'Boxing day'
END Description

FROM DatesAndThursdayInWeek d
-- This join is for getting the week into the result set
inner join Weeks w
on d.Thursday = w.Thursday

OPTION(MAXRECURSION 0)
GO

CREATE FUNCTION Auxiliary.Numbers
(
@AFrom INT,
@ATo INT,
@AIncrement INT
)
RETURNS @RetNumbers TABLE
(
[Number] int PRIMARY KEY NOT NULL
)
AS
BEGIN
WITH Numbers(n)
AS
(
SELECT @AFrom AS n
UNION ALL
SELECT (n + @AIncrement) AS n
FROM Numbers
WHERE
n < @ATo
)
INSERT @RetNumbers
SELECT n from Numbers
OPTION(MAXRECURSION 0)
RETURN;
END
GO

CREATE FUNCTION Auxiliary.iNumbers
(
@AFrom INT,
@ATo INT,
@AIncrement INT
)
RETURNS TABLE
AS
RETURN(
WITH Numbers(n)
AS
(
SELECT @AFrom AS n
UNION ALL
SELECT (n + @AIncrement) AS n
FROM Numbers
WHERE
n < @ATo
)
SELECT n AS Number from Numbers
)
GO

select values of a specific month from table contain timestamp column

select count(transaction_id) tot_tran
,to_char(max(transaction_dte),'Month') month from tbl_trans
where extract (month from transaction_dte)=7

PostgreSQL Extract function explained here

Reference : Date/Time Functions and Operators



Related Topics



Leave a reply



Submit