Givet denne tabel (som du burde have angivet):
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
Forespørgslen skal producere det, du beskriver:
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
CTE'er
kræver Postgres 8.4 eller nyere.
Overvej at opgradere til den seneste punktudgivelse
af 9.1, som i øjeblikket er 9.1.9 .