Skip to main content

Calendar Query in PostgreSQL

Here you can find a query 2011 year calendar and list of holidays(only one per a week(Maharastar Holidays) ) which has been written in PostgreSQL.

QUERY
========

select
min(case when extract(month from d.dd)= 1 then
'Jan'
when extract(month from d.dd)= 2 then
'Feb'
when extract(month from d.dd)=3 then
'Mar'
when extract(month from d.dd)= 4 then
'Apr'
when extract(month from d.dd)= 5 then
'May'
when extract(month from d.dd)= 6 then
'Jun'
when extract(month from d.dd)= 7 then
'Jul'
when extract(month from d.dd)= 8 then
'Aug'
when extract(month from d.dd)=9 then
'Sep'
when extract(month from d.dd)= 10 then
'Oct'
when extract(month from d.dd)= 11 then
'Nov'
when extract(month from d.dd)= 12 then
'Dec'
end
) as MONTH,min(case when extract(DOW from d.dd)=0 then
(extract(day from d.dd))
end) as SUN,
min(case when extract(DOW from d.dd)=1 then
(extract(day from d.dd))
end) as MON,
min(case when extract(DOW from d.dd)=2 then
(extract(day from d.dd))
end) as TUE,
min(case when extract(DOW from d.dd)=3 then
(extract(day from d.dd))
end) as WED,
min(case when extract(DOW from d.dd)=4 then
(extract(day from d.dd))
end) as THU,
min(case when extract(DOW from d.dd)=5 then
(extract(day from d.dd))
end) as FRI,
min(case when extract(DOW from d.dd)=6 then
(extract(day from d.dd))
end) as SAT,
min(case when extract(day from d.dd)=26 and extract(month from d.dd)=1 then
'Republic Day <---> 26th January 2011 <--> Wednessday'
when extract(day from d.dd)=16 and extract(month from d.dd)=2 then
'Id-E-Milad <--> 16th February 2011 <--> Wednessday'
when extract(day from d.dd)=19 and extract(month from d.dd)=2 then
'Chhatrapati Shivaji Maharaj Jayanthi <--> 19th February 2011 <--> Saturday'
when extract(day from d.dd)=2 and extract(month from d.dd)=3 then
'Mahashivratri <--> 2nd March 2011<--> Wednessday'
when extract(day from d.dd)=4 and extract(month from d.dd)=4 then
'Gudi Padwa <--> 4th April 2011 <--> Tuesday'
when extract(day from d.dd)=12 and extract(month from d.dd)=4 then
'Ram Navmi - 12th April - Tuesday'
when extract(day from d.dd)=14 and extract(month from d.dd)=4 then
'Dr. Babasaheb Ambedkar Jayanthi - 14th April - Thursday'
when extract(day from d.dd)=16 and extract(month from d.dd)=4 then
'Mahavir Jayanthi - 16th April - Saturday'
when extract(day from d.dd)=22 and extract(month from d.dd)=4 then
'Good Friday - 22nd April - Friday'
when extract(day from d.dd)=17 and extract(month from d.dd)=5 then
'Buddha Pournima - 17th May - Tuesday'
when extract(day from d.dd)=15and extract(month from d.dd)=8 then
'Independence day - 15th August - Monday'
when extract(day from d.dd)=19 and extract(month from d.dd)=8 then
'Parsi New Year - 19th August - Friday'
when extract(day from d.dd)=31 and extract(month from d.dd)=8 then
'Ramzan Id(Id-Ul-Fitar)(Shawal-1) - 31st August - Wednessday'
when extract(day from d.dd)=1 and extract(month from d.dd)=9 then
'Ganesh Chaturthi - 1st September - Thursday'
when extract(day from d.dd)=6 and extract(month from d.dd)=10 then
'Dasara - 6th October - Thursday'
when extract(day from d.dd)=26 and extract(month from d.dd)=10 then
'Diwali Amavasya (Laxmi Pujan) - 26th October - Wednessday'
when extract(day from d.dd)=27 and extract(month from d.dd)=10 then
'Diwali (Balipratipada) - 27th October - Thursday'
when extract(day from d.dd)=7 and extract(month from d.dd)=11 then
'Bakri Id (iD-Ul-Zun) - 7th November - Monday'
when extract(day from d.dd)=10 and extract(month from d.dd)=11 then
'Guru Nanank Jayanthi - 10th November - Thursday'
when extract(day from d.dd)=6 and extract(month from d.dd)=12 then
'Moharam - 6th December - Tuesday'
when extract(day from d.dd)=20 and extract(month from d.dd)=3 then
'Holi - 20th March - Sunday'
when extract(day from d.dd)=1 and extract(month from d.dd)=5 then
'Maharastra Day - 1st May - Sunday'
when extract(day from d.dd)=2 and extract(month from d.dd)=10 then
'Mahatma Gandhi Jayanthi - 2nd October - Sunday'
when extract(day from d.dd)=25 and extract(month from d.dd)=12 then
'Christmas - 25th December - Sunday'

end) as Holidays
from
(
select
date_trunc('year',current_date)::date + a.si - 1 as dd
from
(
select generate_series(1,cast((extract('day' from date_trunc('year',current_date)-date_trunc('year',current_date-365))) as int)) as si) as a
) as d
group by extract(week from d.dd),extract(month from d.dd)
order by extract(month from d.dd),1,2,3,4,5,6,7;


OUTPUT
========

month | sun | mon | tue | wed | thu | fri | sat | holidays
-------+-----+-----+-----+-----+-----+-----+-----+----------------------------------------------------------------------------
Jan | 2 | | | | | | 1 |
Jan | 9 | 3 | 4 | 5 | 6 | 7 | 8 |
Jan | 16 | 10 | 11 | 12 | 13 | 14 | 15 |
Jan | 23 | 17 | 18 | 19 | 20 | 21 | 22 |
Jan | 30 | 24 | 25 | 26 | 27 | 28 | 29 | Republic Day <---> 26th January 2011 <--> Wednessday
Jan | | 31 | | | | | |
Feb | 6 | | 1 | 2 | 3 | 4 | 5 |
Feb | 13 | 7 | 8 | 9 | 10 | 11 | 12 |
Feb | 20 | 14 | 15 | 16 | 17 | 18 | 19 | Chhatrapati Shivaji Maharaj Jayanthi <--> 19th February 2011 <--> Saturday
Feb | 27 | 21 | 22 | 23 | 24 | 25 | 26 |
Feb | | 28 | | | | | |
Mar | 6 | | 1 | 2 | 3 | 4 | 5 | Mahashivratri <--> 2nd March 2011<--> Wednessday
Mar | 13 | 7 | 8 | 9 | 10 | 11 | 12 |
Mar | 20 | 14 | 15 | 16 | 17 | 18 | 19 | Holi - 20th March - Sunday
Mar | 27 | 21 | 22 | 23 | 24 | 25 | 26 |
Mar | | 28 | 29 | 30 | 31 | | |
Apr | 3 | | | | | 1 | 2 |
Apr | 10 | 4 | 5 | 6 | 7 | 8 | 9 | Gudi Padwa <--> 4th April 2011 <--> Tuesday
Apr | 17 | 11 | 12 | 13 | 14 | 15 | 16 | Dr. Babasaheb Ambedkar Jayanthi - 14th April - Thursday
Apr | 24 | 18 | 19 | 20 | 21 | 22 | 23 | Good Friday - 22nd April - Friday
Apr | | 25 | 26 | 27 | 28 | 29 | 30 |
May | 1 | | | | | | | Maharastra Day - 1st May - Sunday
May | 8 | 2 | 3 | 4 | 5 | 6 | 7 |
May | 15 | 9 | 10 | 11 | 12 | 13 | 14 |
May | 22 | 16 | 17 | 18 | 19 | 20 | 21 | Buddha Pournima - 17th May - Tuesday
May | 29 | 23 | 24 | 25 | 26 | 27 | 28 |
May | | 30 | 31 | | | | |
Jun | 5 | | | 1 | 2 | 3 | 4 |
Jun | 12 | 6 | 7 | 8 | 9 | 10 | 11 |
Jun | 19 | 13 | 14 | 15 | 16 | 17 | 18 |
Jun | 26 | 20 | 21 | 22 | 23 | 24 | 25 |
Jun | | 27 | 28 | 29 | 30 | | |
Jul | 3 | | | | | 1 | 2 |
Jul | 10 | 4 | 5 | 6 | 7 | 8 | 9 |
Jul | 17 | 11 | 12 | 13 | 14 | 15 | 16 |
Jul | 24 | 18 | 19 | 20 | 21 | 22 | 23 |
Jul | 31 | 25 | 26 | 27 | 28 | 29 | 30 |
Aug | 7 | 1 | 2 | 3 | 4 | 5 | 6 |
Aug | 14 | 8 | 9 | 10 | 11 | 12 | 13 |
Aug | 21 | 15 | 16 | 17 | 18 | 19 | 20 | Independence day - 15th August - Monday
Aug | 28 | 22 | 23 | 24 | 25 | 26 | 27 |
Aug | | 29 | 30 | 31 | | | | Ramzan Id(Id-Ul-Fitar)(Shawal-1) - 31st August - Wednessday
Sep | 4 | | | | 1 | 2 | 3 | Ganesh Chaturthi - 1st September - Thursday
Sep | 11 | 5 | 6 | 7 | 8 | 9 | 10 |
Sep | 18 | 12 | 13 | 14 | 15 | 16 | 17 |
Sep | 25 | 19 | 20 | 21 | 22 | 23 | 24 |
Sep | | 26 | 27 | 28 | 29 | 30 | |
Oct | 2 | | | | | | 1 | Mahatma Gandhi Jayanthi - 2nd October - Sunday
Oct | 9 | 3 | 4 | 5 | 6 | 7 | 8 | Dasara - 6th October - Thursday
Oct | 16 | 10 | 11 | 12 | 13 | 14 | 15 |
Oct | 23 | 17 | 18 | 19 | 20 | 21 | 22 |
Oct | 30 | 24 | 25 | 26 | 27 | 28 | 29 | Diwali Amavasya (Laxmi Pujan) - 26th October - Wednessday
Oct | | 31 | | | | | |
Nov | 6 | | 1 | 2 | 3 | 4 | 5 |
Nov | 13 | 7 | 8 | 9 | 10 | 11 | 12 | Bakri Id (iD-Ul-Zun) - 7th November - Monday
Nov | 20 | 14 | 15 | 16 | 17 | 18 | 19 |
Nov | 27 | 21 | 22 | 23 | 24 | 25 | 26 |
Nov | | 28 | 29 | 30 | | | |
Dec | 4 | | | | 1 | 2 | 3 |
Dec | 11 | 5 | 6 | 7 | 8 | 9 | 10 | Moharam - 6th December - Tuesday
Dec | 18 | 12 | 13 | 14 | 15 | 16 | 17 |
Dec | 25 | 19 | 20 | 21 | 22 | 23 | 24 | Christmas - 25th December - Sunday
Dec | | 26 | 27 | 28 | 29 | 30 | 31 |


Comments

Popular posts from this blog

Parallel Operations With pl/pgSQL

Hi, I am pretty sure that, there will be a right heading for this post. For now, i am going with this. If you could suggest me proper heading, i will update it :-) OK. let me explain the situation. Then will let you know what i am trying to do here, and how i did it. Situation here is, We have a table, which we need to run update on “R” no.of records. The update query is using some joins to get the desired result, and do update the table.  To process these “R” no.of records, it is taking “H” no.of hours. That too, it’s giving load on the production server. So, we planned to run this UPDATE as batch process.  Per a batch process, we took “N” no.or records. To process this batch UPDATE, it is taking “S” no.of seconds. With the above batch process, production server is pretty stable, and doing great. So, we planned to run these Batch updates parallel.  I mean, “K” sessions, running different record UPDATEs. Of-course, we can also increase the Batch size here.  But

How To Send E-Mail From PostgreSQL

Hi , If you want to send E-Mails from PostgreSQL, then use the below Python 3.2 Script as below. I have used ActivePython 3.2 with PostgreSQL 9.1 for sending E-Mails from PostgreSQL. If you want to configure the Python 3.2 with PostgreSQL 9.1 then, please refer the below steps. http://manojadinesh.blogspot.in/2012/06/fatal-python-error-pyinitialize-unable.html Once, your Python 3.2 successful then follow the below steps to send an e-mail. Step 1 ===== postgres=# CREATE OR REPLACE FUNCTION public.send_email(_from Text,_password Text,smtp Text,port INT,receiver text, subject text, send_message text) RETURNS TEXT  LANGUAGE plpython3u AS $function$ import smtplib sender = _from receivers = receiver message = ("From: %s\nTo: %s\nSubject: %s\n\n %s"  % (_from,receiver,subject,send_message)) try:   smtpObj = smtplib.SMTP(smtp,port)   smtpObj.starttls()   smtpObj.login(_from, _password)   smtpObj.sendmail(sender, receivers,message)   print ('Successf

::Pipelined in Oracle as well in PostgreSQL::

Pipelined Table Functions:- [ORACLE] =========================== If you want to return multiple rows to the calling environment, then piplined table functions is prefred. It will increase the dbperformance as well. Ex:- Step 1: ----------- CREATE TABLE EMP(EMPNO INT,ENAME VARCHAR2(10),SAL INT); Step 2: ----------- Insert sample data. Step 3: ----------- Create an object for the row type casting. CREATE OR REPLACE TYPE emp_row AS OBJECT ( empno INT, ename VARCHAR2(20), SAL INT ); Step 4: ----------- Create a Return Type for the pipelined function. CREATE OR REPLACE TYPE emp_table_type AS TABLE OF emp_row; Step 5: ----------- CREATE OR REPLACE FUNCTION emp_pipe_function RETURN emp_table_type PIPELINED IS BEGIN FOR rec in (select * from emp) LOOP PIPE ROW (emp_row(rec.empno,rec.ename,rec.sal)); END LOOP; RETURN; END; Step 6: ---------- SQL> select * from table(emp_pipe_function); EMPNO ENAME SAL ---------- ----