如何从Firebird SQL中的日期获取Quarter

时间:2015-11-15 22:32:28

标签: date firebird datepart

我可以轻松获得本月和上个月的总销售额。

SELECT ‘This Mount’,  SUM(Price) FROM Sales 
WHERE EXTRACT(MONTH FROM OrderDate) = EXTRACT(MONTH FROM CURRENT_DATE)   
  AND EXTRACT(YEAR FROM OrderDate) = EXTRACT(YEAR FROM CURRENT_DATE)   
Union All
SELECT ‘Previous Month’,  SUM(Price) FROM Sales 
WHERE EXTRACT(MONTH FROM OrderDate) = EXTRACT(MONTH FROM CURRENT_DATE)   
  AND EXTRACT(YEAR FROM OrderDate) = EXTRACT(YEAR FROM CURRENT_DATE)   

我希望得到本季度和上一季度的总销售额。

使用MS-SQL可以很容易地从日期获取四分之一,如下所示:

SELECT DATEPART(QUARTER, @date)

如何使用Firebird执行此操作?

3 个答案:

答案 0 :(得分:3)

将DECODE功能与EXTRACT结合使用:

SELECT
  DECODE(EXTRACT(MONTH FROM <date_field>),
    1, 'I',
    2, 'I',
    3, 'I',
    4, 'II',
    5, 'II',  
    6, 'II',  
    7, 'III',  
    8, 'III',  
    9, 'III',
    'IV')  
  FROM
    <some_table>

或者只是

  SELECT
    (EXTRACT(MONTH FROM <date_field>) - 1) / 3 + 1
  FROM 
    <some_table>

答案 1 :(得分:1)

os.environ()

此处,&#39;日期&#39;是订单表&#39; CustomerPO&#39;

的字段名称

答案 2 :(得分:0)

SELECT dates,
   EXTRACT(MONTH from dates)            as SalesMonth, 
   ceil(EXTRACT(MONTH from dates) / 3)  as QTR 
from CustomerPO 
where ((dates > '1/1/2016') and (dates < '12/31/2016')) 
order by dates