在Where子句中使用Case时的条款

时间:2017-05-31 17:46:51

标签: sql oracle

我正在使用Oracle进行工作,我有以下脚本,我想加入表a和d中的数据,区号和转移;但是在我的表格中,我没有转变,所以我在使用现金时创建转移语句,但是我必须在表格a中加入创建的转移,但是sql工作并给我我想要的数据,但是我只是想确保这是正确的方法!

select distinct
    a.trn_plan_date as route_date,
    next_day(a.trn_plan_date - 1,'Sunday') as route_week,
    a.trn_zone_code as zone,
    case when to_number(to_char(a.cutoff_datetime,'HH24'))<20 then 'AM' else 'PM' end as shift,
    d.ampm_shift,
    max(d.ups_checkin_time) as Ups,
    d.productivity_region,
    'PLANNED_DEPOT_RUNNER' as hour_type,
    24*(a.truck_endtime -  a.truck_dispatchtime)*count(distinct b.trn_resource_id) as hours,
    c.last_week as last_week_flag,
    c.month_to_date as month_to_date_flag,
    c.last_month as last_month_flag
from
    fd_stg.trn_plan_tra a,
    fd_stg.trn_plan_resource_tra b,
    fd_dw.date_dim c,
    fd_dw.route_dim d
where
    a.trn_plan_id = b.trn_plan_id
    and a.trn_plan_date = c.calendar_date
    and case when to_number(to_char(a.cutoff_datetime,'HH24'))<20 then 'AM' else 'PM' end=d.ampm_shift
    and a.trn_plan_date = d.route_date
    and a.trn_zone_code = d.zone
    and (c.last_month='Y'
        or c.month_to_date='Y' 
        or c.last_week='Y')       
    and (a.trn_region_code = 'Depot' or a.trn_zone_code in('970','971'))
    and b.role = '003'
    and a.trn_zone_code is not null
group by
    a.trn_plan_date,
    a.trn_zone_code,
    case when to_number(to_char(a.cutoff_datetime,'HH24'))<20 then 'AM' else 'PM' end,
    d.productivity_region,
    d.ampm_shift,
    a.truck_endtime - a.truck_dispatchtime,
    c.last_week,
    c.month_to_date,
    c.last_month
  Order by 1,3,4

1 个答案:

答案 0 :(得分:2)

您似乎在询问在where子句中使用CASE .. WHEN是否合适。你有什么是好的。 Gordon正在使用ANSI样式连接。我用ANSI连接(FROM子句)重写了一个例子。

    select distinct
        a.trn_plan_date as route_date,
        next_day(a.trn_plan_date - 1,'Sunday') as route_week,
        a.trn_zone_code as zone,
        case when to_number(to_char(a.cutoff_datetime,'HH24'))<20 then 'AM' else 'PM' end as shift,
        d.ampm_shift,
        max(d.ups_checkin_time) as Ups,
        d.productivity_region,
        'PLANNED_DEPOT_RUNNER' as hour_type,
        24*(a.truck_endtime -  a.truck_dispatchtime)*count(distinct b.trn_resource_id) as hours,
        c.last_week as last_week_flag,
        c.month_to_date as month_to_date_flag,
        c.last_month as last_month_flag
    from
        fd_stg.trn_plan_tra a
        inner join fd_stg.trn_plan_resource_tra b on (a.trn_plan_id = b.trn_plan_id and b.role = '003')
        inner join fd_dw.date_dim c on (a.trn_plan_date = c.calendar_date 
                                          and (c.last_month='Y'
                                            or c.month_to_date='Y' 
                                            or c.last_week='Y') )
        inner join fd_dw.route_dim d on (case when to_number(to_char(a.cutoff_datetime,'HH24'))<20 then 'AM' else 'PM' end=d.ampm_shift
                                            and a.trn_plan_date = d.route_date
                                            and a.trn_zone_code = d.zone)
    where
            (a.trn_region_code = 'Depot' or a.trn_zone_code in('970','971'))
        and a.trn_zone_code is not null
    group by
        a.trn_plan_date,
        a.trn_zone_code,
        case when to_number(to_char(a.cutoff_datetime,'HH24'))<20 then 'AM' else 'PM' end,
        d.productivity_region,
        d.ampm_shift,
        a.truck_endtime - a.truck_dispatchtime,
        c.last_week,
        c.month_to_date,
        c.last_month
      Order by 1,3,4