mathew_s
asked on
Help with clearning syntax error in Complex SQL Server Query
I need some help With SQL Server query. Getting some errors with this query I am trying to re-write. Can someone help to remove the syntax errors? The query in question is the SELECT query...the large one.
Here are some of the syntax errors I am getting in the main query.
Msg 102, Level 15, State 1, Line 209
Incorrect syntax near 'DQS'.
Msg 156, Level 15, State 1, Line 211
Incorrect syntax near the keyword 'AS'.
Msg 102, Level 15, State 1, Line 211
Incorrect syntax near 'tfltr_DATE'.
Msg 156, Level 15, State 1, Line 215
Incorrect syntax near the keyword 'AS'.
Msg 102, Level 15, State 1, Line 215
Incorrect syntax near 'tfltr_DATE'.
Msg 102, Level 15, State 1, Line 221
Incorrect syntax near 'V'.
Msg 102, Level 15, State 1, Line 221
Incorrect syntax near 'tfltr_DATE'.
Msg 102, Level 15, State 1, Line 239
Incorrect syntax near 'ann'.
Msg 102, Level 15, State 1, Line 270
Incorrect syntax near 'DQS'.
Msg 102, Level 15, State 1, Line 272
Incorrect syntax near 'V'.
Msg 102, Level 15, State 1, Line 272
Incorrect syntax near 'tfltr_DATE'.
Msg 102, Level 15, State 1, Line 289
Incorrect syntax near 'ann'.
TEST99.sql
Here are some of the syntax errors I am getting in the main query.
Msg 102, Level 15, State 1, Line 209
Incorrect syntax near 'DQS'.
Msg 156, Level 15, State 1, Line 211
Incorrect syntax near the keyword 'AS'.
Msg 102, Level 15, State 1, Line 211
Incorrect syntax near 'tfltr_DATE'.
Msg 156, Level 15, State 1, Line 215
Incorrect syntax near the keyword 'AS'.
Msg 102, Level 15, State 1, Line 215
Incorrect syntax near 'tfltr_DATE'.
Msg 102, Level 15, State 1, Line 221
Incorrect syntax near 'V'.
Msg 102, Level 15, State 1, Line 221
Incorrect syntax near 'tfltr_DATE'.
Msg 102, Level 15, State 1, Line 239
Incorrect syntax near 'ann'.
Msg 102, Level 15, State 1, Line 270
Incorrect syntax near 'DQS'.
Msg 102, Level 15, State 1, Line 272
Incorrect syntax near 'V'.
Msg 102, Level 15, State 1, Line 272
Incorrect syntax near 'tfltr_DATE'.
Msg 102, Level 15, State 1, Line 289
Incorrect syntax near 'ann'.
TEST99.sql
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi can you please send view ont_sqe_view_energysched
ASKER
Scott - Your solution was very close to fixing it. Now my only issue is down in the area of the select below for the following columns.
MQS.LOC_ID, PD_DQS.LOC_ID, MQS.RESOURCE, PD_DQS.RESOURCE, MQS.AGGREGATE, PD_DQS.AGGREGATE..etc..MQS .entity1_i d, MQS.start_dt, PD_DQS.entity1_id, PD_DQS.start_dt
It is saying "The multi-part identifier <XXXX> could not be bound"
UNION ALL SELECT V.*, CASE WHEN ISNULL(ANN.data_id,0) = 0 THEN 0 ELSE 1 END as has_comm_flg, ANN.ann_id
FROM ( SELECT
'DAILY' SOURCE, ISNULL(ISNULL(ISNULL(DQS.L OC_ID,MQS. LOC_ID),PD _DQS.LOC_I D),PDR_DQS .LOC_ID) LOC_ID,
ISNULL(ISNULL(ISNULL(DQS.R ESOURCE,MQ S.RESOURCE ),PD_DQS.R ESOURCE),P DR_DQS.RES OURCE) RESOURCE,
ISNULL(ISNULL(ISNULL(DQS.A GGREGATE,M QS.AGGREGA TE),PD_DQS .AGGREGATE ),PDR_DQS. AGGREGATE) AGGREGATE,
ISNULL(ISNULL(ISNULL(DQS.S TATION,MQS .STATION), PD_DQS.STA TION),PDR_ DQS.STATIO N) STATION,
ISNULL(ISNULL(ISNULL(DQS.S TATION_GRO UP,MQS.STA TION_GROUP ),PD_DQS.S TATION_GRO UP),PDR_DQ S.STATION_ GROUP) STATION_GROUP,
ISNULL(ISNULL(ISNULL(DQS.S UBMP,MQS.S UBMP),PD_D QS.SUBMP), PDR_DQS.SU BMP) SUBMP,
ISNULL(ISNULL(ISNULL(DQS.D ATE,MQS.DA TE),PD_DQS .DATE),PDR _DQS.DATE) DATE,
ISNULL(ISNULL(ISNULL(DQS.H OUR,MQS.HO UR),PD_DQS .HOUR),PDR _DQS.HOUR) HOUR,
ISNULL(ISNULL(ISNULL(DQS.I NTERVAL,MQ S.INTERVAL ),PD_DQS.I NTERVAL),P DR_DQS.INT ERVAL) INTERVAL,
ISNULL(ISNULL(ISNULL(DQS.Y EARMONTH,M QS.YEARMON TH),PD_DQS .YEARMONTH ),PDR_DQS. YEARMONTH) YEARMONTH,
ISNULL(ISNULL(DQS.CODE,MQS .CODE),PD_ DQS.CODe) CODE,
ISNULL(ISNULL(DQS.CODE2,MQ S.CODE2),P D_DQS.CODE 2) CODE2,
ISNULL(ISNULL(ISNULL(DQS.T AX_ZONE,MQ S.TAX_ZONE ),PD_DQS.T AX_ZONE),P DR_DQS.TAX _ZONE) TAX_ZONE,
ISNULL(ISNULL(ISNULL(DQS.T P_ZONE,MQS .TP_ZONE), PD_DQS.TP_ ZONE),PDR_ DQS.TP_ZON E) TP_ZONE,
DQS.INT_QEI DQSI,
MQS.INT_QEI MQSI,
DQS.INT_QEW DQSW,
MQS.INT_QEW MQSW,
PD_DQS.INT_QEI PD_DQSI,
PD_DQS.INT_QEW PD_DQSW,
PDR_DQS.INT_QEI DA_DQSI,
PDR_DQS.INT_QEW DA_DQSW,
DQS.SCHED_ID DQS_SCHED_ID,
MQS.SCHED_ID MQS_SCHED_ID,
PD_DQS.SCHED_ID PD_DQS_SCHED_ID,
PDR_DQS.SCHED_ID PDR_DQS_SCHED_ID,
PDR_DQS.IHO
FROM ( SELECT V.* FROM (SELECT * FROM #tmp_ontsqeview_211 WHERE sched_type_id=1) V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') DQS
FULL OUTER JOIN
(SELECT V.* FROM (SELECT * FROM #tmp_ontsqeview_1116) V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') PDR_DQS
ON (DQS.entity1_id = PDR_DQS.entity1_id
AND DQS.start_dt = PDR_DQS.start_dt)
OR (MQS.entity1_id = PDR_DQS.entity1_id
AND MQS.start_dt = PDR_DQS.start_dt)
OR (PD_DQS.entity1_id = PDR_DQS.entity1_id
AND PD_DQS.start_dt = PDR_DQS.start_dt) ) V LEFT OUTER JOIN (SELECT data_list_id.data_id, dla.ann_txt, dla.ann_id
MQS.LOC_ID, PD_DQS.LOC_ID, MQS.RESOURCE, PD_DQS.RESOURCE, MQS.AGGREGATE, PD_DQS.AGGREGATE..etc..MQS
It is saying "The multi-part identifier <XXXX> could not be bound"
UNION ALL SELECT V.*, CASE WHEN ISNULL(ANN.data_id,0) = 0 THEN 0 ELSE 1 END as has_comm_flg, ANN.ann_id
FROM ( SELECT
'DAILY' SOURCE, ISNULL(ISNULL(ISNULL(DQS.L
ISNULL(ISNULL(ISNULL(DQS.R
ISNULL(ISNULL(ISNULL(DQS.A
ISNULL(ISNULL(ISNULL(DQS.S
ISNULL(ISNULL(ISNULL(DQS.S
ISNULL(ISNULL(ISNULL(DQS.S
ISNULL(ISNULL(ISNULL(DQS.D
ISNULL(ISNULL(ISNULL(DQS.H
ISNULL(ISNULL(ISNULL(DQS.I
ISNULL(ISNULL(ISNULL(DQS.Y
ISNULL(ISNULL(DQS.CODE,MQS
ISNULL(ISNULL(DQS.CODE2,MQ
ISNULL(ISNULL(ISNULL(DQS.T
ISNULL(ISNULL(ISNULL(DQS.T
DQS.INT_QEI DQSI,
MQS.INT_QEI MQSI,
DQS.INT_QEW DQSW,
MQS.INT_QEW MQSW,
PD_DQS.INT_QEI PD_DQSI,
PD_DQS.INT_QEW PD_DQSW,
PDR_DQS.INT_QEI DA_DQSI,
PDR_DQS.INT_QEW DA_DQSW,
DQS.SCHED_ID DQS_SCHED_ID,
MQS.SCHED_ID MQS_SCHED_ID,
PD_DQS.SCHED_ID PD_DQS_SCHED_ID,
PDR_DQS.SCHED_ID PDR_DQS_SCHED_ID,
PDR_DQS.IHO
FROM ( SELECT V.* FROM (SELECT * FROM #tmp_ontsqeview_211 WHERE sched_type_id=1) V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') DQS
FULL OUTER JOIN
(SELECT V.* FROM (SELECT * FROM #tmp_ontsqeview_1116) V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') PDR_DQS
ON (DQS.entity1_id = PDR_DQS.entity1_id
AND DQS.start_dt = PDR_DQS.start_dt)
OR (MQS.entity1_id = PDR_DQS.entity1_id
AND MQS.start_dt = PDR_DQS.start_dt)
OR (PD_DQS.entity1_id = PDR_DQS.entity1_id
AND PD_DQS.start_dt = PDR_DQS.start_dt) ) V LEFT OUTER JOIN (SELECT data_list_id.data_id, dla.ann_txt, dla.ann_id
Could be helpful to know specifically what "xxxx" was.
You could try selecting all columns, in case column "A" (with 1 in it) is being used. So, instead of V.*, use just *:
/* SELECT * rather than V.* wherever it appears */
FROM (SELECT * FROM #tmp_ontsqeview_211 WHERE sched_type_id=1) V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') DQS
FULL OUTER JOIN
You could try selecting all columns, in case column "A" (with 1 in it) is being used. So, instead of V.*, use just *:
/* SELECT * rather than V.* wherever it appears */
FROM (SELECT * FROM #tmp_ontsqeview_211 WHERE sched_type_id=1) V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') DQS
FULL OUTER JOIN
ASKER
Sorry XXXX was these fields MQS.LOC_ID, PD_DQS.LOC_ID, MQS.RESOURCE, PD_DQS.RESOURCE, MQS.AGGREGATE, PD_DQS.AGGREGATE..etc..MQS .entity1_i d, MQS.start_dt, PD_DQS.entity1_id, PD_DQS.start_dt but not all of them.
ASKER
Didn't make a difference. Same issue. Here is the latest SQL, just left the V.* in.
TEST100.sql
TEST100.sql
Weird. The error is get is:
Msg 207, Level 16, State 1, Line 206
Invalid column name 'IHO'.
Which corresponds to:
PD_DQS.SCHED_ID PD_DQS_SCHED_ID,
PDR_DQS.SCHED_ID PDR_DQS_SCHED_ID,
PDR_DQS.IHO
FROM ( SELECT V.* FROM (SELECT * FROM #tmp_ontsqeview_111 WHERE sched_type_id=1) AS V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') DQS
FULL OUTER JOIN
which makes sense, because the #tmp_ontsqeview_1116 table indeed does not have a column with name "IHO".
Msg 207, Level 16, State 1, Line 206
Invalid column name 'IHO'.
Which corresponds to:
PD_DQS.SCHED_ID PD_DQS_SCHED_ID,
PDR_DQS.SCHED_ID PDR_DQS_SCHED_ID,
PDR_DQS.IHO
FROM ( SELECT V.* FROM (SELECT * FROM #tmp_ontsqeview_111 WHERE sched_type_id=1) AS V INNER JOIN (SELECT 1 A) tfltr_DATE ON V.DATE BETWEEN '20180201' AND '20180201') DQS
FULL OUTER JOIN
which makes sense, because the #tmp_ontsqeview_1116 table indeed does not have a column with name "IHO".
ASKER
Hold on I think I am missing a part of the query at the bottom and that may fix it.
ASKER
Yeah I fixed that problem about IHO, had to create another column in and bring the data.
ASKER
Thanks Scott. Your solution worked for me after I fixed up the remaining stuff. Now to optimize the darn thing!
I can't run the code further because I don't have the other tables:
sett_data_list, etc.
sett_data_list, etc.
Example of bad usage:
Open in new window
Example on how to correct it:
Open in new window