PostgreSQL查询时将时间简化作月份的函数
PostgreSQL查询时将时间简化为月份的函数
功能:数据表tab1中有timestamp类型字段tmp1,如果统计时只用到tmp1字段的年份和月份,可以用下面的函数提高查询效率
-- Function: indexable_timestamp(timestamp without time zone, text)
-- DROP FUNCTION indexable_timestamp(timestamp without time zone, text);
CREATE OR REPLACE FUNCTION indexable_timestamp(timestamp without time zone, text)
RETURNS text AS
$BODY$
select to_char($1,$2)
$BODY$
LANGUAGE sql IMMUTABLE STRICT
COST 100;
ALTER FUNCTION indexable_timestamp(timestamp without time zone, text)
OWNER TO pgsql;
用法:select count(*) from tab1 where indexable_timestamp(tmp1,'yyyy-mm-dd')>'2013-12-01';
功能:数据表tab1中有timestamp类型字段tmp1,如果统计时只用到tmp1字段的年份和月份,可以用下面的函数提高查询效率
-- Function: indexable_timestamp(timestamp without time zone, text)
-- DROP FUNCTION indexable_timestamp(timestamp without time zone, text);
CREATE OR REPLACE FUNCTION indexable_timestamp(timestamp without time zone, text)
RETURNS text AS
$BODY$
select to_char($1,$2)
$BODY$
LANGUAGE sql IMMUTABLE STRICT
COST 100;
ALTER FUNCTION indexable_timestamp(timestamp without time zone, text)
OWNER TO pgsql;
用法:select count(*) from tab1 where indexable_timestamp(tmp1,'yyyy-mm-dd')>'2013-12-01';