MySQL SELECT查询:将28天添加到财务季度的最后一个日期

时间:2015-02-11 14:40:05

标签: mysql select case

我想在一个季度的最后一天增加28天。

我目前有以下内容确定季度时段,如何在本季度的最后一个月添加28天。

CASE    -- Results:  2011-Q4 (Financial Year-Qtr)
    WHEN MONTH(T5.date_purchased) BETWEEN 1  AND 3  THEN concat((YEAR(T5.date_purchased) - 1), '-', 'Q3')
    WHEN MONTH(T5.date_purchased) BETWEEN 4  AND 6  THEN concat((YEAR(T5.date_purchased) - 1), '-', 'Q4')
    WHEN MONTH(T5.date_purchased) BETWEEN 7  AND 9  THEN concat((YEAR(T5.date_purchased) - 0), '-', 'Q1')
    WHEN MONTH(T5.date_purchased) BETWEEN 10 AND 12 THEN concat((YEAR(T5.date_purchased) - 0), '-', 'Q2')
END AS FYrQtr,

我不需要考虑任何不寻常的事情,只需在下个季度的下个月28日。

我已尝试过以下内容,但当然会在订单日期增加28天,而非财务qtr。 (我不需要时间)

CASE    --  Results:  2012-06-20 07:20:07  not  2012-07-28
    WHEN MONTH(T5.date_purchased) BETWEEN 1  AND 3  THEN date_add(T5.date_purchased, Interval 28 day) 
    WHEN MONTH(T5.date_purchased) BETWEEN 4  AND 6  THEN date_add(T5.date_purchased, Interval 28 day)
    WHEN MONTH(T5.date_purchased) BETWEEN 7  AND 9  THEN date_add(T5.date_purchased, Interval 28 day)
    WHEN MONTH(T5.date_purchased) BETWEEN 10  AND 12  THEN date_add(T5.date_purchased, Interval 28 day)
    END AS FRangeQtrBAS,

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

从被购买者那里获得年份,并将其与季度和月份的已知结束联系起来:

CASE    
    WHEN MONTH(T5.date_purchased) BETWEEN 1  AND 3  THEN date_add(CONCAT(YEAR(T5.date_purchased), '-03-31'), Interval 28 day) 
    WHEN MONTH(T5.date_purchased) BETWEEN 4  AND 6  THEN date_add(CONCAT(YEAR(T5.date_purchased), '-06-30'), Interval 28 day)
    WHEN MONTH(T5.date_purchased) BETWEEN 7  AND 9  THEN date_add(CONCAT(YEAR(T5.date_purchased), '-09-30'), Interval 28 day)
    WHEN MONTH(T5.date_purchased) BETWEEN 10  AND 12  THEN date_add(CONCAT(YEAR(T5.date_purchased), '-12-31'), Interval 28 day)
END AS FRangeQtrBAS