SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error when having 2 CTEs


Error when having 2 CTEs

Author
Message
jeffshelix
jeffshelix
SSC Eights!
SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)

Group: General Forum Members
Points: 911 Visits: 201
This code results in
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ','. That is the between the CTEs. I copied syntax from another report that i wrote that works. Thoughts?

;With CTE_1 AS (
SELECT
H.CUSTOMS_ENTRY_NUM as [Entry Num]
,L.Entry_Line_ID as Line
,H.[Broker_Invoice]
,H.Entry_Type
,L.PART_NUM AS [ESL Part]
,L.HTS_NUM AS [ESL HTS]
,CAST(L.ENTRY_DATE AS DATE) AS [Entry_Date]
,C.[CLASS_VALUE_1] as [P/C_HTS]
,C.[CLASSIFIED_DATE] as [P/C Class Date]
FROM
ADHOC.ATS_ESH H
INNER JOIN ADHOC.ATS_ESL L
ON H.TRANS_SK = L.TRANS_SK
/* Joining on Prod_Class */
LEFT JOIN [TSC].[EMS_TVC_PROD_CLASS] C
ON L.[PART_NUM] = Replace(C.[PROD_ID],'-','') AND C.[CTRY_CODE] = 'US' AND C.CLASS_TYPE = 'HS' and C.BUS_UNIT = 'FORD' AND C.[CLASS_DETAIL_LEVEL] = 'Import_Export'
WHERE (H.Importer = 'FORD'
AND H.Entry_Summary_Date > GetDate () - 30
AND L.HTS_Num NOT LIKE '98%'
AND L.Part_Num <> 'NPN'
AND H.Com_Desc_Code = 'Parts'
AND Substring(L.Part_Num,4,1) <> 'Z'
AND L.HTS_NUM <> ISNULL(C.[CLASS_VALUE_1], '')
)
, CTE_2 AS (
SELECT
Z.[COMPOSITE_PART]
,Z.HTS_NUMBER
,MAX(Z.CREATED_DATE) AS MAX_DATE
FROM [TSI].[ZATS_BROKER_FEED] Z
INNER JOIN ADHOC.ATS_ESL L
ON L.PART_NUM = Z.COMPOSITE_PART
AND Z.CREATED_DATE <= L.ENTRY_DATE
WHERE
Z.SUB_ORG='FORD'
GROUP BY
Z.COMPOSITE_PART
,Z.HTS_NUMBER
)

Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: General Forum Members
Points: 147422 Visits: 22091
jeffshelix - Monday, March 5, 2018 12:34 PM
This code results in
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ','. That is the between the CTEs. I copied syntax from another report that i wrote that works. Thoughts?

;With CTE_1 AS (
SELECT
H.CUSTOMS_ENTRY_NUM as [Entry Num]
,L.Entry_Line_ID as Line
,H.[Broker_Invoice]
,H.Entry_Type
,L.PART_NUM AS [ESL Part]
,L.HTS_NUM AS [ESL HTS]
,CAST(L.ENTRY_DATE AS DATE) AS [Entry_Date]
,C.[CLASS_VALUE_1] as [P/C_HTS]
,C.[CLASSIFIED_DATE] as [P/C Class Date]
FROM
ADHOC.ATS_ESH H
INNER JOIN ADHOC.ATS_ESL L
ON H.TRANS_SK = L.TRANS_SK
/* Joining on Prod_Class */
LEFT JOIN [TSC].[EMS_TVC_PROD_CLASS] C
ON L.[PART_NUM] = Replace(C.[PROD_ID],'-','') AND C.[CTRY_CODE] = 'US' AND C.CLASS_TYPE = 'HS' and C.BUS_UNIT = 'FORD' AND C.[CLASS_DETAIL_LEVEL] = 'Import_Export'
WHERE (H.Importer = 'FORD'
AND H.Entry_Summary_Date > GetDate () - 30
AND L.HTS_Num NOT LIKE '98%'
AND L.Part_Num <> 'NPN'
AND H.Com_Desc_Code = 'Parts'
AND Substring(L.Part_Num,4,1) <> 'Z'
AND L.HTS_NUM <> ISNULL(C.[CLASS_VALUE_1], '')
)
, CTE_2 AS (
SELECT
Z.[COMPOSITE_PART]
,Z.HTS_NUMBER
,MAX(Z.CREATED_DATE) AS MAX_DATE
FROM [TSI].[ZATS_BROKER_FEED] Z
INNER JOIN ADHOC.ATS_ESL L
ON L.PART_NUM = Z.COMPOSITE_PART
AND Z.CREATED_DATE <= L.ENTRY_DATE
WHERE
Z.SUB_ORG='FORD'
GROUP BY
Z.COMPOSITE_PART
,Z.HTS_NUMBER
)


For starters, you're missing the SELECT that comes after a CTE. Without it you'll always get an error.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: General Forum Members
Points: 147422 Visits: 22091
You're also either missing the closing parenthesis for the first CTE or having an extra opening parenthesis after the WHERE clause.

EDIT: Did I mention that you have several code smells that suggest a bad database design? If possible, you should correct them.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
jeffshelix
jeffshelix
SSC Eights!
SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)SSC Eights! (911 reputation)

Group: General Forum Members
Points: 911 Visits: 201
You guys rock. I just had two CTEs , , , i didnt do the Select after CTE2 , , ,
Thanks!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search