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


issue joining on Temp Table


issue joining on Temp Table

Author
Message
jeffshelix
jeffshelix
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 175
crashes at
/* Joining on Prod_Class */ section.
Issue has something to do with joining on a created temp table.
thoughts?

SELECT * INTO #TEMP1 From
(Select
L.PART_NUM
,Like_PD_ID
,PD_ID
from
ADHOC.ATS_ESH H
INNER JOIN ADHOC.ATS_ESL L
ON H.TRANS_SK = L.TRANS_SK
Inner Join [TSC].[EMS_PDLINK] P
on P.USER_VARCHAR_4 = L.Part_Num
WHERE
H.Importer = 'Ford'
and H.Entry_Summary_Date > '12/1/2017'
AND L.HTS_Num NOT LIKE N'98%'
AND L.Part_Num NOT LIKE N'NPN'
AND H.Com_Desc_Code = 'Parts'
AND Substring(L.Part_Num,4,1) Like 'Z'
) as Service

SELECT DISTINCT
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(H.ENTRY_DATE AS DATE) AS [Entry_Date]
,C.[CLASS_VALUE_1] as [P/C_HTS]
,C.[CLASSIFIED_DATE] as [P/C Class Date]
,P.Status as [Work Queue Status]
--,T.TSI_PROD_ID as [TA Prod ID]
--,T.HTS_NUM as [TA HTS]
--,ISNULL(T.UPDATE_DATE,T.INSERT_DATE) as [TA Update]
,Z.[HTS_NUMBER] as [Zats HTS]
,Z.[INSERT_DATE] as [Zats Date]
--,z.composite_part as [Zats Part]
--,B.[CLASS_VALUE_1] as [Hist HTS]
--,B.[Classified_Date] as [Hist Date]
----,B.[PROD_ID] as Hist_Prod_ID
--,CASE
-- WHEN Substring(L.Part_Num,4,1) = 'Z'
-- THEN 'Service'
-- END as Service
,A.PD_id as [PDLink Serv Part]
,A.[LIKE_PD_ID] as [PDLink Eng Part]
,[IRQ].[TCM_INRQ].[INFO_REQUEST_ID]
,[IRQ].[TCM_INRQ].ENTRY_NUM as [IRQ Entry]
,[IRQ].[TCM_INRQ_DTL].[REQUEST_TYPE]
,replace(replace([Request_NOTE], char(10), ''), char(13), '') as Request_Note
,replace(replace([Response_NOTE], char(10), ''), char(13), '') as Response_Note
,replace(replace([INTERNAL_NOTE], char(10), ''), char(13), '') as Internal_Note
,CASE
When A.[LIKE_PD_ID] is not Null
THEN 'Possible PDLINK Issue'
WHEN C.[CLASS_VALUE_1] IS NULL
THEN 'Part Not in Classifier'
WHEN Z.[HTS_NUMBER] IS NULL
THEN 'Part Not on Broker Feed'
WHEN Z.[HTS_NUMBER] <> C.[CLASS_VALUE_1]
THEN 'Prod Class not same as Broker Feed'
END as Comment
,CASE
WHEN L.HTS_NUM = Z.[HTS_NUMBER]
THEN 'Broker Used Broker Feed'
END as Comment_2

/* Pulls Entry Data */
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 #TEMP1.PD_ID = 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'

--/* Joining on Prod_Class History */
--LEFT JOIN [TSC].[EMS_TVC_PROD_CLASS_HIST] B
--ON L.[PART_NUM] = Replace(B.[PROD_ID],'-','') and B.[CTRY_CODE] = 'US' and B.CLASS_TYPE = 'HS' and B.[BUS_UNIT] = 'FORD' AND B.[CLASS_DETAIL_LEVEL] = 'Import_Export'
--and B.CLASSIFIED_DATE > Getdate() - 365

/* Joining on Work Queue */
LEFT JOIN [TSC].[EMS_TVC_WRK_QUEUE] P
ON L.[PART_NUM] = Replace(P.[PROD_ID],'-','') and P.CLASS_TYPE = 'HS' and P.[BUS_UNIT] = 'FORD'

--/* Joining on TSI (TA)*/
--LEFT JOIN
--TSI.TSI_PD_HS T
--ON L.[PART_NUM] = Replace(T.[TSI_PROD_ID],'-','')
--and T.INSERT_DATE > Getdate() -365

/* Joining on PD Link to pull Engineering Part */
LEFT JOIN [TSC].[EMS_PDLINK] A
ON L.[PART_NUM] = Replace(A.Like_PD_ID,'-','') and A.[SUB_ORG] = 'FORD'

/* Joining on ZATS Broker Feed */
LEFT JOIN
[TSI].[ZATS_BROKER_FEED] Z
on L.Part_Num = Z.Composite_part and Z.[SUB_ORG] = 'FORD' and Z.Insert_date > Getdate() - 365

/* Joining on IRQ */
Left Join [IRQ].[TCM_INRQ]
on H.[Compressed Entry_Num] = [IRQ].[TCM_INRQ].Entry_Num

Inner Join [IRQ].[TCM_INRQ_DTL]
ON [IRQ].[TCM_INRQ].[INRQ_SK] = [IRQ].[TCM_INRQ_DTL].[INRQ_SK]

Where
H.Importer = N'FORD'
AND H.Entry_Summary_Date >= '11/1/2017'
AND H.Entry_Summary_Date < '12/1/2017'
AND L.HTS_Num NOT LIKE N'98%'
AND L.Part_Num NOT LIKE N'NPN'
AND L.HTS_NUM NOT LIKE ISNULL(C.[CLASS_VALUE_1], '')
AND H.Com_Desc_Code = 'Parts'
AND Substring(L.Part_Num,4,1) Like 'Z'

ORDER BY
L.PART_NUM,
CAST(H.ENTRY_DATE AS DATE),
C.[CLASSIFIED_DATE],
Z.[INSERT_DATE]
ZZartin
ZZartin
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16287 Visits: 15391
Well it doesn't look like you actually joined the temp table into the query anywhere, you're just using it in the join criteria.
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116626 Visits: 21715
Well, you're referencing the temp table without joining to it first.


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
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 175
Thanks
starting with...
/* Joining on Prod_Class */
left JOIN [TSC].[EMS_TVC_PROD_CLASS] C
ON #TEMP1.PD_ID = C.[PROD_ID]

I need the value (Class_value_1) from Prod_Class where #TEMP1.PD_ID = C.[PROD_ID]

I appreciate the reply.
jeffshelix
jeffshelix
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 175
"Well, you're referencing the temp table without joining to it first. "

I do not understand what this means or how to fix it . . . .thanks
ZZartin
ZZartin
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16287 Visits: 15391
jeffshelix - Wednesday, January 3, 2018 9:03 AM
"Well, you're referencing the temp table without joining to it first. "

I do not understand what this means or how to fix it . . . .thanks

It means you have to treat the temp table the same way as any other table in the query, you can't just include it in the join criteria without actually well joining to the table first in some way.

jeffshelix
jeffshelix
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 175
ISnt this code joining the tables?

left JOIN [TSC].[EMS_TVC_PROD_CLASS] C
ON #TEMP1.PD_ID = C.[PROD_ID]
ZZartin
ZZartin
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16287 Visits: 15391
jeffshelix - Wednesday, January 3, 2018 9:10 AM
ISnt this code joining the tables?

left JOIN [TSC].[EMS_TVC_PROD_CLASS] C
ON #TEMP1.PD_ID = C.[PROD_ID]

Nope, that is joining to [TSC].[EMS_TVC_PROD_CLASS] and referencing #TEMP1 in the join criteria but you haven't actually joined to #TEMP1 which is why it's complaining.

jeffshelix
jeffshelix
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 175
Ah, the light is flickering . . . thanks, i will try that . . . .

Thanks for your patience and quick reply!!!!!!!!!!!!!!!!

Jeff
GilaMonster
GilaMonster
SSC Guru
SSC Guru (674K reputation)SSC Guru (674K reputation)SSC Guru (674K reputation)SSC Guru (674K reputation)SSC Guru (674K reputation)SSC Guru (674K reputation)SSC Guru (674K reputation)SSC Guru (674K reputation)

Group: General Forum Members
Points: 674601 Visits: 48433
JOIN #Temp ON ...
Then you can reference it further down. Same as any other table.

You can't say
FROM Customers inner join OrderDetails on Customers.ID = Orders.CustomerID, same with temp tables.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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