将Oracle SQL语句转换为SQL Server语句

时间:2017-07-05 18:02:02

标签: sql sql-server tsql

预订项目现在需要相同的数据提取 - 但是来自SQL Server数据库 - 而不是Oracle。任何人都可以协助将以下内容转换为SQL Server语法吗?

SELECT *
FROM (
            SELECT o.ot_outlet_code
                             ,v.lab_site_code ot_outlet_code
                             ,v.brand
                             ,v.region
                            , bd.cd_day_date booking_date, dd.cd_day_date dining_date
                            , f.last_change_date, f.created_date
                            , f.modified_date, t15.ts_timeslot_desc
                           , t.TIME, s.session_type
                            , tbs.booking_status, f.ADDED_BY_USER
                            , bp.product, bs.booking_source
                            , f.SPECIAL_OFFER, f.SEATING_PREFERENCE
                            , f.Tables_guest_id, covers
                            , booking_occurrence, breakfast_flag
                            , row_number() OVER (PARTITION BY f.Tables_guest_id ORDER BY f.last_change_date DESC, f.last_change_time DESC) rank_latest_record
                            , f.title, f.emailoptout
                            , f.MOBILE_OPT_IN, f.HIGH_CHAIR_COVERS
                            , f.GUEST_TYPE, f.Booking_ID
            FROM owbi.whs_fact_rest_booking f
                            , owbi.whs_dim_cal_date bd
                            , owbi.whs_dim_cal_date dd
                            , owbi.whs_dim_bat_booking_source bs
                            , owbi.whs_dim_time_of_day t
                            , owbi.whs_dim_bat_product bp
                            , owbi.whs_dim_15_timeslot t15
                            , owbi.whs_dim_bat_booking_status tbs
                            , owbi.whs_dim_bat_session s
                            , owbi.bat_restaurants_v v  
       WHERE f.whs_dim_outlet = v.outlet
    AND f.whs_dim_booking_date = bd.dimension_Key 
    AND f.whs_dim_dining_date = dd.dimension_key 
    AND f.whs_dim_bat_session = s.dimension_key 
    AND f.whs_dim_bat_booking_status = tbs.dimension_key 
    AND f.whs_dim_bat_product = bp.dimension_Key 
    AND f.whs_dim_bat_booking_source = bs.dimension_key 
    AND f.whs_dim_booking_time = t.dimension_Key 
    AND f.whs_dim_dining_15_timeslot = t15.dimension_key
    AND dd.ey_year_code in ('2018')
    AND f.whs_dim_dining_date >= 20170303   
        )
WHERE rank_latest_record = 1
ORDER BY BOOKING_DATE DESC;

1 个答案:

答案 0 :(得分:0)

派生表必须具有别名。例如

SELECT *
FROM (
            SELECT o.ot_outlet_code
                             ,v.lab_site_code ot_outlet_code
                             ,v.brand
                             ,v.region
                            , bd.cd_day_date booking_date, dd.cd_day_date dining_date
                            , f.last_change_date, f.created_date
                            , f.modified_date, t15.ts_timeslot_desc
                           , t.TIME, s.session_type
                            , tbs.booking_status, f.ADDED_BY_USER
                            , bp.product, bs.booking_source
                            , f.SPECIAL_OFFER, f.SEATING_PREFERENCE
                            , f.Tables_guest_id, covers
                            , booking_occurrence, breakfast_flag
                            , row_number() OVER (PARTITION BY f.Tables_guest_id ORDER BY f.last_change_date DESC, f.last_change_time DESC) rank_latest_record
                            , f.title, f.emailoptout
                            , f.MOBILE_OPT_IN, f.HIGH_CHAIR_COVERS
                            , f.GUEST_TYPE, f.Booking_ID
            FROM owbi.whs_fact_rest_booking f
                            , owbi.whs_dim_cal_date bd
                            , owbi.whs_dim_cal_date dd
                            , owbi.whs_dim_bat_booking_source bs
                            , owbi.whs_dim_time_of_day t
                            , owbi.whs_dim_bat_product bp
                            , owbi.whs_dim_15_timeslot t15
                            , owbi.whs_dim_bat_booking_status tbs
                            , owbi.whs_dim_bat_session s
                            , owbi.bat_restaurants_v v  
       WHERE f.whs_dim_outlet = v.outlet
    AND f.whs_dim_booking_date = bd.dimension_Key 
    AND f.whs_dim_dining_date = dd.dimension_key 
    AND f.whs_dim_bat_session = s.dimension_key 
    AND f.whs_dim_bat_booking_status = tbs.dimension_key 
    AND f.whs_dim_bat_product = bp.dimension_Key 
    AND f.whs_dim_bat_booking_source = bs.dimension_key 
    AND f.whs_dim_booking_time = t.dimension_Key 
    AND f.whs_dim_dining_15_timeslot = t15.dimension_key
    AND dd.ey_year_code in ('2018')
    AND f.whs_dim_dining_date >= 20170303   
        ) dt
WHERE rank_latest_record = 1
ORDER BY BOOKING_DATE DESC;

在SQL Server中,它被认为是不良的形式,不使用ANSI样式的JOIN,尽管对于内部联接来说,将它们作为交叉连接与WHERE子句中的连接条件一起使用是完全合法的。

在FROM子句中使用CTE而不是子查询/内联视图/派生表通常更好。