TERADATA案例何时在WHERE子句中声明

时间:2017-05-18 20:10:50

标签: teradata where-clause case-when

我需要在WHERE子句中编写一个case语句,它是 - 当current_date是月的第1个时,则选择上个月的第1天和上个月的上个月之间的数据 从Curr月的第1天到日期。到目前为止我写过这个但是它没有用。 ' 05/01/2017'将是输入日期。

SELECT *
FROM    MyTable
WHERE calendar_date
    BETWEEN
        CASE WHEN extract (day from CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY')) =1 --check to see date is 1st of month
        THEN  ADD_MONTHS((CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY') - EXTRACT(DAY FROM CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY'))+1), -1) --1st of prev month
                AND ADD_MONTHS(CURRENT_DATE - EXTRACT(DAY FROM CURRENT_date), 0 ) --last day prev month
        ELSE             CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY') - EXTRACT(DAY FROM CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY'))+1, 0) --else 1st of Curr mont

        AND CURRENT_DATE
    end
    order by calendar_date

2 个答案:

答案 0 :(得分:1)

select      *

from        mytable

where       calendar_date   between case 
                                        when td_day_of_month (current_date) = 1
                                        then current_date - interval '1' month
                                        else td_month_begin (current_date)
                                    end

                            and     case 
                                        when td_day_of_month (current_date) = 1
                                        then current_date - 1
                                        else current_date 
                                    end


order by    calendar_date

答案 1 :(得分:0)

@Dudu,根据你的建议,我能够解决sql问题。这是:

real*8 :: dt=2.0d0**(-5)