2017-05-31 82 views
0

我使用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 
+0

*從不*在'FROM'子句中使用逗號。 *總是*使用正確的,明確的'JOIN'語法。 –

+0

謝謝,我會關注它;不過你能告訴我腳本里有什麼不對嗎,特別是我用case的時候where子句一起加入換班 –

回答

2

您似乎在問是否在where子句中使用CASE .. WHEN是合適的。你有什麼可以。戈登指的是使用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 
+0

非常感謝你的幫助! –