我想选择sql:SELECT "year-month" from table group by "year-month" AND order by date
,其中year-month - format为日期"1978-01","1923-12“。选择couse work的to_char,但不是"right“order:
to_char(timestamp_column, 'YYYY-MM')
发布于 2010-12-26 04:35:23
date_part(text, timestamp)
例如:
date_part('month', timestamp '2001-02-16 20:38:40'),
date_part('year', timestamp '2001-02-16 20:38:40')
http://www.postgresql.org/docs/8.0/interactive/functions-datetime.html
发布于 2013-06-24 16:55:38
to_char(timestamp, 'YYYY-MM')
您说顺序不“正确”,但我不明白为什么它是错误的(至少在公元10000年到来之前)。
发布于 2014-08-10 02:39:58
使用date_trunc
方法截断日期(或您想要的任何其他内容,例如,周、年、日等)。
按月对订单进行销售分组的示例:
select
SUM(amount) as sales,
date_trunc('month', created_at) as date
from orders
group by date
order by date DESC;
https://stackoverflow.com/questions/4531577
复制相似问题