【问题标题】:Order By Month Name - Postgresql按月份名称排序 - Postgresql
【发布时间】:2021-09-14 15:53:52
【问题描述】:

我关注了这个帖子Order by using month name in PostgreSQL,但没有成功!

我有一个查询(它的工作),我只需要按 mont 名称对结果进行排序。这是我正在使用的查询:

   select to_char(purchase_date, 'Month') as mes_2021,
           sum(gmv::float4) as soma_gmv
           from tablename
    where purchase_date > '2021-01-01'
    GROUP BY mes_2021

我正在尝试:

order by to_date(purchase_date, 'Month') - No success
order by date_part(purchase_date::date, 'Month') - No success

如果我使用order by mes_2021

【问题讨论】:

  • 您的代码有什么原因不包括 1 月 1 日吗?
  • 不确定我是否理解,但一月份就在那里,对吧?哦,现在我看到 '>' 而不是 '>=' 谢谢
  • 。 . purchase_date > '2021-01-01'.

标签: sql postgresql


【解决方案1】:

一个技巧是在日期上使用窗口函数:

select to_char(purchase_date, 'Month') as mes_2021,
       sum(gmv::float4) as soma_gmv
from tablename
where purchase_date > '2021-01-01'
group by mes_2021
order by min(purchase_date);

当然,这假设日期都在同一年。但是您的 where 子句正在处理这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多