我有一个带有日期列的表。我想得到数月的数量,并按月计算。月份应显示为' Jan',' 2月'如果我使用to_char函数,则顺序发生在文本上。我可以使用extract(从dt开始的月份),但这也会以数字格式显示月份。这是报告的一部分,月份应显示在“周一”中。格式化。
SELECT to_char(dt,'Mon'), COUNT(*) FROM tb GROUP BY to_char(dt,'Mon') ORDER BY to_char(dt,'Mon');
to_char | count
---------+-------
Dec | 1
Jan | 1
Jul | 2
答案 0 :(得分:2)
select month, total
from (
select
extract(month from dt) as month_number,
to_char(dt,'mon') as month,
count(*) as total
from tb
group by 1, 2
) s
order by month_number