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


CTE Issue, part 2


CTE Issue, part 2

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

Group: General Forum Members
Points: 905 Visits: 201
This was working, but with a few Duplicate rows from the ZATS table (which i can live with if i have to)
Now i am getting the crazy execution plan with MILLIONS of rows.
Can anyone figure this out?
CTE_1 has a Part_Num and HTS code, and an Entry Date.
I want to pull the HTS_NUM in the row with the Maximum Created_Date from CTE_2 that is prior to the ENTRY_DATE in CTE_1.

I am to the point I will pay someone! Lol
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
,L.HTS_NUM
,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], '')
) --SELECT * FROM cte_1 ORDER BY [Entry Num], Line

, CTE_2 AS (
SELECT DISTINCT
cte_1.Part_Num
,CTE_1.Entry_Date
,Z.[COMPOSITE_PART]
,Z.HTS_NUMBER
,MAX(Z.CREATED_DATE) as Zats_Date
--,Z.CREATED_DATE as Zats_Date
FROM [TSI].[ZATS_BROKER_FEED] Z
INNER JOIN CTE_1
ON CTE_1.PART_NUM = Z.COMPOSITE_PART
AND Z.CREATED_DATE <= CTE_1.ENTRY_DATE
WHERE Z.SUB_ORG='FORD'
GROUP BY cte_1.Part_Num,
CTE_1.Entry_Date,
Z.COMPOSITE_PART
,Z.HTS_NUMBER
) --SELECT * FROM CTE_2

Select
[Entry Num]
,Line
,Broker_Invoice
,Entry_Type
,c1.Part_Num
,HTS_Num
,CTE_2.Entry_Date
,CTE_2.[COMPOSITE_PART]
,CTE_2.HTS_NUMBER as Zats_HTS_NUMBER
,MAX(Zats_Date)
--,Zats_Date
From CTE_1 c1
left join CTE_2
on c1.PART_NUM = CTE_2.COMPOSITE_PART
group by [Entry Num]
,Line
,Broker_Invoice
,Entry_Type
,c1.Part_Num
,HTS_Num
,CTE_2.Entry_Date
,CTE_2.[COMPOSITE_PART]
,CTE_2.HTS_NUMBER

thanks
Jeff
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)SSC Guru (352K reputation)

Group: General Forum Members
Points: 352443 Visits: 41673
jeffshelix - Monday, March 5, 2018 2:34 PM
This was working, but with a few Duplicate rows from the ZATS table (which i can live with if i have to)
Now i am getting the crazy execution plan with MILLIONS of rows.
Can anyone figure this out?
CTE_1 has a Part_Num and HTS code, and an Entry Date.
I want to pull the HTS_NUM in the row with the Maximum Created_Date from CTE_2 that is prior to the ENTRY_DATE in CTE_1.

I am to the point I will pay someone! Lol
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
,L.HTS_NUM
,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], '')
) --SELECT * FROM cte_1 ORDER BY [Entry Num], Line

, CTE_2 AS (
SELECT DISTINCT
cte_1.Part_Num
,CTE_1.Entry_Date
,Z.[COMPOSITE_PART]
,Z.HTS_NUMBER
,MAX(Z.CREATED_DATE) as Zats_Date
--,Z.CREATED_DATE as Zats_Date
FROM [TSI].[ZATS_BROKER_FEED] Z
INNER JOIN CTE_1
ON CTE_1.PART_NUM = Z.COMPOSITE_PART
AND Z.CREATED_DATE <= CTE_1.ENTRY_DATE
WHERE Z.SUB_ORG='FORD'
GROUP BY cte_1.Part_Num,
CTE_1.Entry_Date,
Z.COMPOSITE_PART
,Z.HTS_NUMBER
) --SELECT * FROM CTE_2

Select
[Entry Num]
,Line
,Broker_Invoice
,Entry_Type
,c1.Part_Num
,HTS_Num
,CTE_2.Entry_Date
,CTE_2.[COMPOSITE_PART]
,CTE_2.HTS_NUMBER as Zats_HTS_NUMBER
,MAX(Zats_Date)
--,Zats_Date
From CTE_1 c1
left join CTE_2
on c1.PART_NUM = CTE_2.COMPOSITE_PART
group by [Entry Num]
,Line
,Broker_Invoice
,Entry_Type
,c1.Part_Num
,HTS_Num
,CTE_2.Entry_Date
,CTE_2.[COMPOSITE_PART]
,CTE_2.HTS_NUMBER

thanks
Jeff


We could use the DDL (CREATE TABLE statement) for the tables involved, including indexes, and the execution plan (as an *.sqlplan file).

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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