Convert Oracle SQL statement into SQL Server statement - sql

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.

Related

sql count new id that did not exists before for each month

I have the follow set of data
enter image description here
how can I write the sql to gives the result on right side?
that is the counting of unique id that did appeared previously for each month.
After long time of reading and reading his question, Ssiu wanted to ask the following:
So here is the test data in MS SQL: at that time he didn't clarify on postgresql
create table tmp1 (
ddate datetime
, iid int
)
insert into tmp1 values
('2017-11-01',1)
,('2017-11-02',2)
,('2017-11-03',3)
,('2017-11-04',4)
,('2017-11-05',5)
,('2017-11-06',5)
,('2017-11-07',5)
,('2017-12-01',1)
,('2017-12-02',2)
,('2017-12-03',3)
,('2017-12-04',6)
,('2017-12-05',7)
,('2018-01-01',1)
,('2018-01-02',2)
,('2018-01-03',3)
,('2018-01-04',4)
,('2018-01-05',8)
Disclaimer: The following is not the best approach for this problem. It is not applicable for more months, however it can give Ssiu a clue.
with cte(mmonth, iid) as (
select distinct convert(varchar(7), ddate, 120) mmonth
, iid
from tmp1
)
, cte_201711 as (
select * from cte where mmonth = '2017-11'
)
, cte_201712 as (
select * from cte where mmonth = '2017-12'
)
, cte_201801 as (
select * from cte where mmonth = '2018-01'
)
, cte_cnt201712 as(
select cte_201711.mmonth as mm201711
, cte_201711.iid as id201711
, cte_201712.mmonth as mm201712
, cte_201712.iid as id201712
from cte_201711
full outer join cte_201712
on cte_201712.iid = cte_201711.iid
)
, cte_cnt201801 as (
select cte_201711.mmonth as mm201711
, cte_201711.iid as id201711
, cte_201712.mmonth as mm201712
, cte_201712.iid as id201712
, cte_201801.mmonth as mm201801
, cte_201801.iid as id201801
from cte_201711
full outer join cte_201712
on cte_201712.iid = cte_201711.iid
full outer join cte_201801
on cte_201801.iid = cte_201712.iid
or cte_201801.iid = cte_201711.iid
)
--select * from cte_cnt201801 order by isnull(mm201711,'z'), isnull(mm201712,'z')
select '2017-12' mmonth, count(*) Ssiu
from cte_cnt201712
where mm201711 is null
union all
select '2018-01' mmonth, count(*) Ssiu
from cte_cnt201801
where mm201711 is null
and mm201712 is null
Note the data for the cte_cnt201801 CTE:
select * from cte_cnt201801 order by isnull(mm201711,'z'), isnull(mm201712,'z')
So the result for the above query is:

SQL Server Select one column by aggregate but not another

Not good with sql. Forgive me if the question isn't 100% clear. Here is my query
SELECT
MAX(PatientId),
[Date],
[Time],
CASE WHEN MAX(CAST(HealthScoreSkipped as INT)) = 1
THEN '--'
ELSE MAX(DailyHealthScore)
END DailyHealthScore,
ProtocolGroupName,
MAX(BloodPressure) BloodPressure,
MAX(SystolicAlert) SystolicAlert,
MAX(DiastolicAlert) DiastolicAlert,
MAX(BloodPressureSkipped) BloodPressureSkipped,
MAX(Pulse) Pulse,
MAX(PulseAlert) PulseAlert,
MAX(PulseSkipped) PulseSkipped,
MAX(BloodSugar) BloodSugar,
MAX(BloodSugarAlert) BloodSugarAlert,
MAX(BloodSugarSkipped) BloodSugarSkipped,
MAX(Steps) Steps,
MAX(StepsAlert) StepsAlert,
MAX(StepsSkipped) StepsSkipped,
MAX(O2) O2,
MAX(O2Alert) O2Alert,
MAX(O2Skipped) O2Skipped,
MAX(Weight) Weight,
MAX(WeightAlert) WeightAlert,
#BaselineWeight AS BaselineWeight,
MAX(WeightSkipped) WeightSkipped,
MAX(Temperature) Temperature,
MAX(TemperatureAlert) TemperatureAlert,
MAX(TemperatureUnit) TemperatureUnit,
MAX(TemperatureSkipped) TemperatureSkipped,
MAX(PEF) PEF,
MAX(PEFAlert) PEFAlert,
MAX(PEFSkipped) PEFSkipped,
MAX(FEV1) FEV1,
MAX(FEV1Alert) FEV1Alert,
MAX(FEV1Skipped) FEV1Skipped,
MAX(FEVRatio) FEVRatio,
MAX(FEVRatioAlert) FEVRatioAlert,
MAX(FEVRatioSkipped) FEVRatioSkipped,
#SpiroEnabled SpiroEnabled
FROM #bioAndScores
GROUP BY PatientId, Date, Time, ProtocolGroupName
The problem here is on the lines
MAX(Steps) Steps,
MAX(StepsAlert) StepsAlert
I want to select the max Steps but the stepalert value that goes with that row not the max of the stepAlert.
You can create a sub query in the select statement to get the steps alert that corresponds to your step.
something along the lines of the below (note that I'm not sure why you are grouping by patientId, if you are taking the max(patientId) if you do want to group by patient id, the where clause of the sub query should also match on patient Id
SELECT
MAX(bas.PatientId),
bas.[Date],
bas.[Time],
bas.ProtocolGroupName,
.
.
.
MAX(bas.Steps) Steps,
--sub query to get the StepsAlert that corresponds to max steps
(SELECT
StepsAlert
FROM
#bioAndScores subBas
WHERE
--This is the important part of finding the match for Max Steps
MAX(bas.Steps) = subBas.Steps AND
--commented out because the MAX(PatientId) was ambiguous
--bas.PatientId = subBas.PatientId AND
bas.[Date] = subBas.[Date] AND
bas.[Time] = subBas.[Time] AND
bas.ProtocolGroupName = subBas.ProtocolGroupName) as StepsAlert
FROM
#bioAndScores as bas
GROUP BY
--PatientId,
bas.Date,
bas.Time,
bas.ProtocolGroupName
Remove the MAX() function from StepAlerts and add StepAlerts to your GROUP BY clause.
MAX(Steps) AS Steps,
StepsAlert AS StepsAlert
And in your GROUP BY:
GROUP BY PatientId, Date, Time, ProtocolGroupName, StepAlerts
Just add StepsAlert column to the group by clause and remove the MAX aggregate function.
GROUP BY PatientId, Date, Time, ProtocolGroupName,StepsAlert
I would suggest you go through this to better understand about how group by works.
You can do this using apply() to select the set of values that correspond to the highest Steps at the earliest StepsAlert like so:
select
PatientId
, [Date]
, [Time]
, DailyHealthScore = case
when MAX(CAST(HealthScoreSkipped as int))= 1 then '--'
else MAX(DailyHealthScore)
end
, ProtocolGroupName
, BloodPressure = MAX(BloodPressure)
, SystolicAlert = MAX(SystolicAlert)
, DiastolicAlert = MAX(DiastolicAlert)
, BloodPressureSkipped= MAX(BloodPressureSkipped)
, Pulse = MAX(Pulse)
, PulseAlert = MAX(PulseAlert)
, PulseSkipped = MAX(PulseSkipped)
, BloodSugar = MAX(BloodSugar)
, BloodSugarAlert = MAX(BloodSugarAlert)
, BloodSugarSkipped = MAX(BloodSugarSkipped)
, Steps = x.Steps
, StepsAlert = x.StepsAlert
, StepsSkipped = MAX(StepsSkipped)
, O2 = MAX(O2)
, O2Alert = MAX(O2Alert)
, O2Skipped = MAX(O2Skipped)
, Weight = MAX(Weight)
, WeightAlert = MAX(WeightAlert)
, BaselineWeight = #BaselineWeight
, WeightSkipped = MAX(WeightSkipped)
, Temperature = MAX(Temperature)
, TemperatureAlert = MAX(TemperatureAlert)
, TemperatureUnit = MAX(TemperatureUnit)
, TemperatureSkipped = MAX(TemperatureSkipped)
, PEF = MAX(PEF)
, PEFAlert = MAX(PEFAlert)
, PEFSkipped = MAX(PEFSkipped)
, FEV1 = MAX(FEV1)
, FEV1Alert = MAX(FEV1Alert)
, FEV1Skipped = MAX(FEV1Skipped)
, FEVRatio = MAX(FEVRatio)
, FEVRatioAlert = MAX(FEVRatioAlert)
, FEVRatioSkipped = MAX(FEVRatioSkipped)
, SpiroEnabled = #SpiroEnabled
from #bioAndScores b
cross apply (
select top 1
i.Steps
, i.StepsAlert
from #bioAndScores i
where b.PatientId = i.PatientId
and b.[Date] = i.[Date]
and b.[Time] = i.[Time]
and b.ProtocolGroupName = i.ProtocolGroupName
order by i.Steps desc, i.StepsAlert asc
) x
group by
PatientId
, date
, time
, ProtocolGroupName

ORA-30926: unable to get a stable set of rows in the source tables

I have a customer who gets: ORA-30926: unable to get a stable set of rows in the source tables:
Log show error Massage Error (30926)
13:52:19 (00:00:02.406) ERROR : Error (30926) (00:00:02.406) ORA-30926: Stabile Zeilengruppe in den Quelltabellen kann nicht eingelesen werden
TS03_MIN0100: UpdTable failed. Update inv_value in cMinTimeTable:
MERGE INTO HUBWBPMS5_ENTTS03005400223 a USING ( SELECT DISTINCT a.inv_value +
( a.inv_value_sum - h.inv_value ) AS inv_value , a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h , HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no AND a.sequence_no = h.max_seq_no
AND a.client = h.client ) xzfd_t ON ( xzfd_t.xzfd_rid = a.rowid )
WHEN MATCHED THEN
UPDATE
SET a.inv_value = xzfd_t.inv_value
I have checked for duplicate values in the tables but cant find anything unusual.
Maybe someone has an idea that could be useful.
The query is:
Query causing error (temp table):
INSERT INTO HUBWBPMS5_ENTTS03005700228 ( agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , inc_ref , inv_value , max_seq_no , pd , period , project , resource_id , resource_typ , trans_date , unit , voucher_no , work_order , work_type )
SELECT agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , inc_ref , SUM ( inv_value ) inv_value , max_seq_no , pd , period , project , resource_id , resource_typ , trans_date , unit , voucher_no , work_order , work_type
FROM HUBWBPMS5_ENTTS03005400228
WHERE agg_flag = 1
GROUP BY agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , period , trans_date , voucher_no , max_seq_no , inc_ref , pd , project , resource_id , resource_typ , unit , work_order , work_type
When you get that error, it will be from a MERGE statement, and it indicates that there are multiple rows in the source dataset that match to a row you're joining to in the target table, and as such, Oracle doesn't know which one to use to do the update.
Taking your merge statement:
MERGE INTO HUBWBPMS5_ENTTS03005400223 a
USING (SELECT DISTINCT a.inv_value + ( a.inv_value_sum - h.inv_value ) AS inv_value,
a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h,
HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no
AND a.sequence_no = h.max_seq_no
AND a.client = h.client) xzfd_t
ON (xzfd_t.xzfd_rid = a.rowid)
WHEN MATCHED THEN
UPDATE SET a.inv_value = xzfd_t.inv_value;
it looks like the join between the two tables HUBWBPMS5_ENTTS03005700223 and HUBWBPMS5_ENTTS03005400223 in the xzfd_t subquery causes multiple rows to be returned for one or more of the HUBWBPMS5_ENTTS03005400223 rows (ie. you get multiple rows returned for at least one a.rowid).
To check this, run:
SELECT xzfd_rid,
COUNT(*) cnt
FROM (SELECT DISTINCT a.inv_value + ( a.inv_value_sum - h.inv_value ) AS inv_value,
a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h,
HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no
AND a.sequence_no = h.max_seq_no
AND a.client = h.client)
GROUP BY xzfd_rid
HAVING COUNT(*) > 1;
In order to fix this, you'd need to make the xzfd_t subquery return a single row for each xzfd_rid. Possibly using row_number() to pick a single row, or an aggregate query to sum up all the h.inv_value fields per a.rowid instead of the DISTINCT.

Column 'city.POPULATION' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I was creating a query, and i saw this error-message.
This is the query i'm trying to execute:
select D.ID_city
, D.ID_year
, D.NB_deaths
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, COUNT( NB_births_M+ NB_births_F)/C.population as Birthrate
, COUNT(NB_deaths_M + NB_deaths_F) as Mortality
from Demography D
join region C
on (D.Id_city =C.Id_city)
group
by D.ID_city
, D.ID_year
, D.NB_deaths_M
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
You should include c.population in the group by, since you use that column outside the count aggregate function:
COUNT( NB_births_M+ NB_births_F)/C.population
Use this (added D.NB_deaths_F too since it is necessary too):
select D.ID_city
, D.ID_year
, D.NB_deaths
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, COUNT( NB_births_M+ NB_births_F)/C.population as Birthrate
, COUNT(NB_deaths_M + NB_deaths_F) as Mortality
from Demography D
join region C
on (D.Id_city =C.Id_city)
group
by D.ID_city
, D.ID_year
, D.NB_deaths_M
, D.NB_deaths_F -- <-- added
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, C.population -- <-- added

Odd WHERE NOT EXISTS performance on DB2

I am experiencing very odd performance on DB2 version 9.1 when running the query below:
select a.CYCL_NUM
, a.AC_NUM
, a.AUTHS_DTE
, a.PL_ID
, a.APRVD_RSPN_CDE
, a.AUTHS_AMT
, a.AUTHS_STS_CDE
, a.TRAN_CTGR_CDE
, a.MRCHN_CTGR_CDE
, d.out_pu_au_amt
from nwhd12.chldr_auths a, nwhd12.w_chldr_ac d
where cycl_num = 200911
and a.ac_num = d.ac_num
and APRVD_RSPN_CDE = 'APV'
and not exists (
select 1 from auths_rev_hist b
where a.cycl_num = b.cycl_num
and a.auths_dte = b.auths_dte
and a.TRAN_CTGR_CDE = b.TRAN_CTGR_CDE
and a.PL_ID = b.pl_id
and a.APRVD_RSPN_CDE = b.APRVD_RSPN_CDE
and a.AUTHS_AMT = b.auths_amt
and a.TRAN_CTGR_CDE = b.TRAN_CTGR_CDE
and a.MRCHN_CTGR_CDE = MRCHN_CTGR_CDE
)
;
What is supposed to happen is that the query accesses partion 97 of nwhd12.chldr_auths, since that is the partition corresponding to cycle 200911. Instead, after accessing partition 97, it starts accessing every other partition in nwhd12.chldr_auths. Now, I was told that this is because of the "WHERE NOT EXISTS", but there is still the restriction on cycles in this statement (a.cycl_num = b.cycl_num), so why is it scanning all the partitions?
If I hard code the cycle in the where not exists, then the query performs as expected.
Thanks,
Dave
if the planner is this easily confused, you need to try a few different formulations. this untested (I don't even have DB2, but CTEs originated there):
WITH hist AS (
cycl_num
, ac_num
, auths_dte
, pl_id
, aprvd_rspn_cde
, auths_amt
, auths_sts_cde
, tran_ctgr_cde
, mrchn_ctgr_cde
FROM auths_rev_hist b
)
, auths AS (
SELECT
cycl_num
, ac_num
, auths_dte
, pl_id
, aprvd_rspn_cde
, auths_amt
, auths_sts_cde
, tran_ctgr_cde
, mrchn_ctgr_cde
FROM nwhd12.chldr_auths
WHERE cycl_num = 200911
AND aprvd_rspn_cde = 'APV'
EXCEPT
SELECT ... FROM hist
)
SELECT a.*, d.out_pu_au_amt
FROM auths a, nwhd12.w_chldr_ac d
WHERE a.ac_num = d.ac_num