Re: crosstab

Поиск
Список
Период
Сортировка
От dinesh kumar
Тема Re: crosstab
Дата
Msg-id CALnrH7rE_Aaak=BVTEArkJDsyOmQZYD6hbAjJMXtOQiTpVvBKw@mail.gmail.com
обсуждение исходный текст
Ответ на crosstab  (punnoose <punnoose.pj@dwisesolutions.com>)
Список pgsql-general
Hi , 

I believe, we need to have a Dynamic SQL for this to generate the dynamic Crostab.. I have tried the same earlier and wrote the below Calendar Query for the year 2011 including holidays. 

Crostab Example 
=============
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 |


Best Regards,
Dinesh


On Tue, Sep 4, 2012 at 9:09 PM, punnoose <punnoose.pj@dwisesolutions.com> wrote:
hi all
How could i use crostab to display variable number of columns. in the output
There could be variable number of columns
Regards
Punnoose



--
View this message in context: http://postgresql.1045698.n5.nabble.com/crosstab-tp5722632.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

В списке pgsql-general по дате отправления:

Предыдущее
От: punnoose
Дата:
Сообщение: crosstab
Следующее
От: Steve Crawford
Дата:
Сообщение: Re: "Too far out of the mainstream"