如何按时间顺序而不是按字母顺序按月份名称排序?

how to order by month name chronologically instead of alphabetically?

我想让月份名称按时间顺序排列,而不是按字母顺序排列。这是我的 Sql 代码。

select  SUM(montant_ht)as Achat ,monthname(dim_date.date) as mois 
from operation_odoo  
join dim_date on operation_odoo.id_date_facturation = dim_date.id_date
where (operation_odoo.id_type_op=1) and (dim_date.Annee= ? or ? is null )
group by mois

您可以使用按月排序()

    select  
        SUM(montant_ht)as Achat 
    ,monthname(dim_date.date) as mois 
    from operation_odoo  
    join dim_date on operation_odoo.id_date_facturation = dim_date.id_date
    where (operation_odoo.id_type_op=1) and (dim_date.Annee= ? or ? is null )
    group by mois
    order by month(dim_date.date)

对于 SpagoBI,请尝试将列 month() 添加到查询中

select  
    SUM(montant_ht)as Achat 
,monthname(dim_date.date) as mois 
,  month(dim_date.date)
from operation_odoo  
join dim_date on operation_odoo.id_date_facturation = dim_date.id_date
where (operation_odoo.id_type_op=1) and (dim_date.Annee= ? or ? is null )
group by mois,  month(dim_date.date)
order by month(dim_date.date)

您可以按月订购。我会推荐:

order by month(max(dim_date.date))

关于您的问题,我建议:

select monthname(d.date) as mois,
       sum(o.montant_ht)as Achat 
from operation_odoo o join
     dim_date d
     on o.id_date_facturation = d.id_date
where o.id_type_op = 1 and
      ( d.Annee= ? or ? is null )
group by mois
order by month(max(d.date));

在 select 查询中包含用于月份编号和按月份编号排序的附加列 确保你在分组依据中也包含相同的内容

示例查询:

select current_Date,DATE_FORMAT(current_date,'%b') as 
m_name,DATE_FORMAT(current_date,'%m') as m_num
order by m_num