简体   繁体   中英

Convert Oracle SQL statement into SQL Server statement

A bookings project now requires the same data extract - but from an SQL Server database - instead of Oracle. Can anyone assist converting the following into SQL Server syntax?

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;

The derived table must have an alias. eg

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;

In SQL Server it's considered poor form not use ANSI-style JOINs, although it's perfectly legal for inner joins to write them as a cross-join with the join criteria in the WHERE clause.

And it's generally better to use CTEs instead of subqueries/inline views/derived tables in the FROM clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM