Search Postgresql Archives

Re: Find Number Of Sundays Between Two Dates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



am  Tue, dem 04.03.2008, um 22:16:07 -0800 mailte raghukumar folgendes:
> 
> Hai EverBody,
> 
>         Can I know what is the query by which we can find the number of
> sundays between two given dates in postgres

No problem, for instance for month january 2008:

test=*# select count(1) from 
  (select '2008-01-01'::date + s*'1day'::interval as datum from 
    generate_series(0,30) s)foo 
  where extract(dow from datum)=6;
 count
-------
     4
(1 row)


With generate_series() i generate a list of dates, and later i check if
the date are a saturday. Okay, you need to know sunday -> change from 6
to 0 and ou course, you can calculate the parameter for the
generate_series like 

test=*# select count(1) from 
  (select '2008-01-01'::date + s*'1day'::interval as datum from 
    generate_series(0,'2008-01-31'::date - '2008-01-01'::date) s)foo 
  where extract(dow from datum)=0;
 count
-------
     4
(1 row)




Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux