有自定义年份,而不是日历年

时间:2016-02-10 17:05:39

标签: sql postgresql

我希望这一年从August开始,然后在以下July结束。除了编写我每年必须写的丑陋IFELSE语句之外,还有一种简单的方法吗?理想情况下,我希望有一年如2009-102010-11等。我使用Postgres

以下是我想操作以获取自定义年份的示例查询。

Select extract(year from created) as Year, type, count(*) 
from table
group by Year, type
order by Year

任何帮助将不胜感激,TIA。

3 个答案:

答案 0 :(得分:3)

考虑设置日期表。使用此日期表,您可以将各种数据与每个日期相关联,例如会计年度,季度,月份#,是假日,周末等。

答案 1 :(得分:0)

SELECT (CASE WHEN (EXTRACT(month FROM created) > 6) THEN CAST(EXTRACT(year FROM created) AS text) || '-' || CAST(EXTRACT(year FROM created) +1 AS text) ELSE (CAST(EXTRACT(year FROM created) - 1 AS text) || '-' || CAST(EXTRACT(year FROM created) AS text)) END) AS CustomYear
FROM asmt.testscores order by created desc LIMIT 10000

这似乎已经成功了。

答案 2 :(得分:0)

select
    extract(year from created + interval '6 months') as "Year",
    type,
    count(*) 
from table
group by 1, 2
order by 1