lunedì 8 marzo 2010

PSQL: Month-Based Filtering

My need was to delete some DB entries based on the month field of the date. So I did used the function date_part of PostgreSQL, like this:

SELECT id FROM mytable WHERE date_part('month', date) = 3 AND date_part('year', date) = 2010 and datasetid = 356497950 GROUP BY id;

Now, to delete these entries, just nest this query in a DELETE FROM, like this:

DELETE FROM mytable WHERE id IN (SELECT id FROM mytable WHERE date_part('month', date) = 3 AND date_part('year', date) = 2010 AND datasetid = 356497950 GROUP BY id);

Nessun commento:

Posta un commento