按联邦财政年度重新排序月份

Posted

技术标签:

【中文标题】按联邦财政年度重新排序月份【英文标题】:Re-Ordering the Months By Federal Fiscal Year 【发布时间】:2015-04-13 17:55:51 【问题描述】:

运行以下查询时,我试图找到一种方法来按联邦财政年度显示返回的月份,而不是正常的顺序值。

(即我想按以下顺序显示月份 Oct、Nov、Dec、Jan、Feb、Mar、Apr、May、Jun、Jul、Aug、Sept 而不是 Jan 到 Dec。)谢谢

select wrkgrp,
    sum (case when extract(month from reportdate) = 1 then 1 else 0 end) as January,
    sum (case when extract(month from reportdate) = 2 then 1 else 0 end) as February,
    sum (case when extract(month from reportdate) = 3 then 1 else 0 end) as March,
    sum (case when extract(month from reportdate) = 4 then 1 else 0 end) as April,
    sum (case when extract(month from reportdate) = 5 then 1 else 0 end) as May,
    sum (case when extract(month from reportdate) = 6 then 1 else 0 end) as June,
    sum (case when extract(month from reportdate) = 7 then 1 else 0 end) as July,
    sum (case when extract(month from reportdate) = 8 then 1 else 0 end) as August,
    sum (case when extract(month from reportdate) = 9 then 1 else 0 end) as September,
    sum (case when extract(month from reportdate) = 10 then 1 else 0 end) as October,
    sum (case when extract(month from reportdate) = 11 then 1 else 0 end) as November,
    sum (case when extract(month from reportdate) = 12 then 1 else 0 end) as December,
from workorder
where reportdate between to_date ('2014-10-01 00:00:00', 'yyyy/mm/dd hh24:mi:ss')
and   to_date ('2015-09-30 00:00:00', 'yyyy/mm/dd hh24:mi:ss')
and   wrkgrp = 'PublicWorks'
group by 'wrkgrp;'

【问题讨论】:

【参考方案1】:

这些字段将按照您在选择语句中列出的顺序(水平)显示在您的结果中。像这样首先列出 Oct 来构建您的语句:

select wrkgrp,
sum (case when extract(month from reportdate) = 10 then 1 else 0 end) as October,
sum (case when extract(month from reportdate) = 11 then 1 else 0 end) as November,
sum (case when extract(month from reportdate) = 12 then 1 else 0 end) as December,
sum (case when extract(month from reportdate) = 1 then 1 else 0 end) as January,
sum (case when extract(month from reportdate) = 2 then 1 else 0 end) as February,
sum (case when extract(month from reportdate) = 3 then 1 else 0 end) as March,
sum (case when extract(month from reportdate) = 4 then 1 else 0 end) as April,
sum (case when extract(month from reportdate) = 5 then 1 else 0 end) as May,
sum (case when extract(month from reportdate) = 6 then 1 else 0 end) as June,
sum (case when extract(month from reportdate) = 7 then 1 else 0 end) as July,
sum (case when extract(month from reportdate) = 8 then 1 else 0 end) as August,
sum (case when extract(month from reportdate) = 9 then 1 else 0 end) as September
from workorder
where reportdate between to_date ('2014-10-01 00:00:00', 'yyyy/mm/dd hh24:mi:ss') ad to_date ('2015-09-30 00:00:00', 'yyyy/mm/dd hh24:mi:ss') and
wrkgrp = 'PublicWorks'
group by 'wrkgrp;'

【讨论】:

【参考方案2】:

在 order by 子句中添加 case 语句以获得所需的排序。

ORDER BY 
CASE WHEN extract(month from reportdate) = 10 THEN 1 
CASE WHEN extract(month from reportdate) = 11 THEN 2 
ASC 

【讨论】:

以上是关于按联邦财政年度重新排序月份的主要内容,如果未能解决你的问题,请参考以下文章

数学计算财政年度 5-4-4 时间表

获取财政年度查询中的动态日期范围的逻辑

在 PostgreSQL 中,选择会计年度月份格式作为日期列

ANSI SQL 按(印度)财政年度 YTD 分组 - (过去 5 年 YTD)

纳税年度内的增量

西澳拼命挖矿,联邦预算公布中只字未提西澳功绩,州长失望;预计2022年或重开澳洲国境,留学生迎来好消息!退役波音飞机在重新起航