This question is based of two previous here and here.
I am trying very hard to get those two queries:
SELECT min(to_date(nullif(mydatetext,\'\'), \'D
Given this table (like you should have provided):
CREATE TEMP TABLE tbl (
id int PRIMARY KEY
,mydatetxt text
);
INSERT INTO tbl VALUES
(1, '01.02.2011')
,(2, '05.01.2011')
,(3, '06.03.2012')
,(4, '07.08.2011')
,(5, '04.03.2013')
,(6, '06.08.2011')
,(7, '') -- empty string
,(8, '02.02.2013')
,(9, '04.06.2010')
,(10, '10.10.2012')
,(11, '04.04.2012')
,(12, NULL) -- NULL
,(13, '04.03.2013'); -- min date a 2nd time
The query should produce what you describe:
result as minimal date, maximal date, count of dates between and including min and max dates
WITH base AS (
SELECT to_date(mydatetxt, 'DD.MM.YYYY') AS the_date
FROM tbl
WHERE mydatetxt <> '' -- excludes NULL and ''
)
SELECT min(the_date) AS dmin
,max(the_date) AS dmax
,count(*) AS ct_incl
,(SELECT count(*)
FROM base b1
WHERE b1.the_date < max(b.the_date)
AND b1.the_date > min(b.the_date)
) AS ct_excl
FROM base b
-> SQLfiddle demo
CTEs require Postgres 8.4 or later.
Consider to upgrade to the latest point release of 9.1, which is currently 9.1.9.