Instead of typing 'NXT_US.dbo.' over and over, how else could I do this?
SELECT DISTINCT
NXT_US.dbo.icsp.prod, NXT_US.dbo.icsp.descrip##1, NXT_US.dbo.icsp.descrip##2, NXT_US.dbo.icsw.whse, NXT_US.dbo.sastn.descrip AS branch, NXT_US.dbo.icsw.qtyonhand, NXT_US.dbo.icsp.unitstock AS UOM, NXT_US.dbo.apsv.vendno AS VendorNumber,
NXT_US.dbo.apsv.name AS VendorName, NXT_US.dbo.icsp.prodcat, NXT_US.dbo.sasta.descrip, NXT_US.dbo.icsw.prodline, NXT_US.dbo.icsw.statustype, NXT_US.dbo.icsw.lastinvdt, NXT_US.dbo.oeel.invoicedt, NXT_US.dbo.icsw.avgcost, NXT_US.dbo.oeel.vendno,
NXT_US.dbo.icsw.avgcost * NXT_US.dbo.icsw.qtyonhand AS ExtCost, NXT_US.dbo.oeel.prodcost, NXT_US.dbo.oeel.orderno
INTO InventoryView
FROM NXT_US.dbo.sasta INNER JOIN
NXT_US.dbo.icsp ON NXT_US.dbo.sasta.codeval = NXT_US.dbo.icsp.prodcat LEFT OUTER JOIN
NXT_US.dbo.icswu ON NXT_US.dbo.icsp.prod = NXT_US.dbo.icswu.prod LEFT OUTER JOIN
NXT_US.dbo.icsw LEFT OUTER JOIN
NXT_US.dbo.oeel ON NXT_US.dbo.icsw.lastinvdt = NXT_US.dbo.oeel.invoicedt AND NXT_US.dbo.icsw.whse = NXT_US.dbo.oeel.whse AND NXT_US.dbo.icsw.prod = NXT_US.dbo.oeel.shipprod RIGHT OUTER JOIN
NXT_US.dbo.sastn RIGHT OUTER JOIN
NXT_US.dbo.icsd ON NXT_US.dbo.sastn.cono = NXT_US.dbo.icsd.cono AND NXT_US.dbo.sastn.codeval = NXT_US.dbo.icsd.divno ON NXT_US.dbo.icsw.whse = NXT_US.dbo.icsd.whse ON NXT_US.dbo.icsp.prod = NXT_US.dbo.icsw.prod LEFT OUTER JOIN
NXT_US.dbo.apsv ON NXT_US.dbo.icsw.arpvendno = NXT_US.dbo.apsv.vendno
WHERE (NXT_US.dbo.sasta.cono = '1') AND (NXT_US.dbo.sasta.codeiden = 'c') AND (NXT_US.dbo.icsw.statustype = 's') AND (NXT_US.dbo.icsp.cono = '1') AND (NXT_US.dbo.sastn.codeiden = 'V') and NXT_US.dbo.icsw.whse = 'G010'
Alias each table the first time you use it in your FROM clause. Then you can use the alias in your SELECT, WHERE, GROUP BY clauses. Here is the example (but use better aliases than the generic ones I put here):
SELECT DISTINCT
t2.prod, t2.descrip##1, t2.descrip##2, t4.whse, t6.descrip AS branch, t4.qtyonhand, t2.unitstock AS UOM, t8.vendno AS VendorNumber,
t8.name AS VendorName, t2.prodcat, t1.descrip, t4.prodline, t4.statustype, t4.lastinvdt, t5.invoicedt, t4.avgcost, t5.vendno,
t4.avgcost * t4.qtyonhand AS ExtCost, t5.prodcost, t5.orderno
INTO InventoryView
FROM NXT_US.dbo.sasta AS t1 INNER JOIN
NXT_US.dbo.icsp AS t2 ON t1.codeval = t2.prodcat LEFT OUTER JOIN
NXT_US.dbo.icswu AS t3 ON t2.prod = t3.prod LEFT OUTER JOIN
NXT_US.dbo.icsw AS t4 LEFT OUTER JOIN
NXT_US.dbo.oeel AS t5 ON t4.lastinvdt = t5.invoicedt AND t4.whse = t5.whse AND t4.prod = t5.shipprod RIGHT OUTER JOIN
NXT_US.dbo.sastn AS t6 RIGHT OUTER JOIN
NXT_US.dbo.icsd AS t7 ON t6.cono = t7.cono AND t6.codeval = t7.divno ON t4.whse = t7.whse ON t2.prod = t4.prod LEFT OUTER JOIN
NXT_US.dbo.apsv AS t8 ON t4.arpvendno = t8.vendno
WHERE (t1.cono = '1') AND (t1.codeiden = 'c') AND (t4.statustype = 's') AND (t2.cono = '1') AND (t6.codeiden = 'V') and t4.whse = 'G010'
Related
I have the below query, and for some reason the Left Outer Join to PO_HEADERS_ALL POH is causing the error ORA-00918: column ambiguously defined on Left Outer Join. I don't have any columns selected from this table that would be causing this error from what I see. If I comment out this join then the query validates successfully. Appreciate any assistance on this, thanks!
SELECT
ESIB.ITEM_NUMBER,
ESIT.LONG_DESCRIPTION,
IOP.ORGANIZATION_CODE,
IOQD.SUBINVENTORY_CODE,
SUM(
IOQD.PRIMARY_TRANSACTION_QUANTITY
) ONHAND_QTY,
ESIB.PRIMARY_UOM_CODE ONHAND_UOM,
UOM.UOM_CODE TO_UOM,
INTRACONV.CONVERSION_RATE
, SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5 LOCATOR
,hp.party_name MFG
,etp.tp_item_number MFG_PART_NUM
FROM
EGP_SYSTEM_ITEMS_B ESIB
LEFT OUTER JOIN EGP_SYSTEM_ITEMS_TL ESIT ON ESIB.INVENTORY_ITEM_ID = ESIT.INVENTORY_ITEM_ID
AND ESIB.ORGANIZATION_ID = ESIT.ORGANIZATION_ID --Ended
INNER JOIN INV_ONHAND_QUANTITIES_DETAIL IOQD ON ESIB.INVENTORY_ITEM_ID = IOQD.INVENTORY_ITEM_ID
AND ESIB.ORGANIZATION_ID = IOQD.ORGANIZATION_ID
LEFT OUTER JOIN inv_item_locations LOC ON inventory_location_id= IOQD.locator_id
LEFT OUTER JOIN egp_item_relationships_b eir ON eir.inventory_item_id = ESIB.inventory_item_id and eir.item_relationship_type = 'MFG_PART_NUM'
LEFT OUTER JOIN egp_trading_partner_items etp ON etp.tp_item_id = eir.tp_item_id
and etp.tp_type = 'MANUFACTURER'
LEFT OUTER JOIN hz_parties hp ON hp.party_id = etp.trading_partner_id
LEFT OUTER JOIN PO_LINES_ALL POL ON POL.ITEM_ID = ESIB.INVENTORY_ITEM_ID
--ERROR IS OCCURING WITH THE FOLLOWING JOIN:
LEFT OUTER JOIN PO_HEADERS_ALL POH ON POH.PO_HEADER_ID = POL.PO_HEADER_ID
INNER JOIN INV_UNITS_OF_MEASURE_B UOM ON UOM.BASE_UOM_FLAG = 'Y'
LEFT OUTER JOIN INV_UOM_CONVERSIONS INTRACONV ON --INTRACONV.UOM_CODE = IOQD.TRANSACTION_UOM_CODE
INTRACONV.UOM_CODE = ESIB.PRIMARY_UOM_CODE
AND UOM.UOM_CLASS = INTRACONV.UOM_CLASS
AND INTRACONV.INVENTORY_ITEM_ID = IOQD.INVENTORY_ITEM_ID
--AND INTRACONV.UOM_CODE = UOM.UOM_CODE
INNER JOIN INV_ORG_PARAMETERS IOP ON ESIB.ORGANIZATION_ID = IOP.ORGANIZATION_ID
WHERE
(
INTRACONV.UOM_CODE = IOQD.TRANSACTION_UOM_CODE
OR ESIB.PRIMARY_UOM_CODE = UOM.UOM_CODE
)
GROUP BY
ESIB.ITEM_NUMBER,
ESIT.LONG_DESCRIPTION,
IOP.ORGANIZATION_CODE,
IOQD.SUBINVENTORY_CODE,
ESIB.PRIMARY_UOM_CODE,
UOM.UOM_CODE,
INTRACONV.CONVERSION_RATE
, SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
,hp.party_name
,etp.tp_item_number
I am writing an equivalent logic in BQ as in my source system. In source SQL server side it is working fine. But in Big query it is failing with the OR condition in the last left outer join condition. If I am moving the OR condition in the where clause it is giving wrong count. Need help to fix this issue. How can I re write the below query ?
SELECT count(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON (DIM.diSet = PRO.diSet)
INNER JOIN DQConfig CFG
ON (CFG.ConSet = PRO.ConSet)
LEFT OUTER JOIN AgSt CCT
ON (CCT.StSet = PRO.StSet)
INNER JOIN stprof SummPRO
ON (SummPRO.diSet = DIM.SummdiSet AND
SummPRO.dIntervalStart = PRO.dIntervalStart AND
SummPRO.SiteId = PRO.SiteId AND
SummPRO.nDuration = PRO.nDuration)
LEFT OUTER JOIN AgSt SummCCT
ON (SummCCT.StSet = SummPRO.StSet)
LEFT OUTER JOIN AgentStatus SummSTS
ON (
SummSTS.StSet = SummPRO.StSet
OR
SummSTS.StSet = PRO.StSet)
WHERE DIM.cType = 'A'
You can replace LEFT JOIN with CROSS JOIN and move condition from ON clause to WHERE clause as in below example
#standardSQL
SELECT COUNT(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON DIM.diSet = PRO.diSet
INNER JOIN DQConfig CFG
ON CFG.ConSet = PRO.ConSet
LEFT OUTER JOIN AgSt CCT
ON CCT.StSet = PRO.StSet
INNER JOIN stprof SummPRO
ON SummPRO.diSet = DIM.SummdiSet
AND SummPRO.dIntervalStart = PRO.dIntervalStart
AND SummPRO.SiteId = PRO.SiteId
AND SummPRO.nDuration = PRO.nDuration
LEFT OUTER JOIN AgSt SummCCT
ON SummCCT.StSet = SummPRO.StSet
CROSS JOIN AgentStatus SummSTS
WHERE DIM.cType = 'A'
AND (
SummSTS.StSet = SummPRO.StSet
OR SummSTS.StSet = PRO.StSet
)
I experienced a strange behaviour, and I already was thinking of looking for a proper shrink. Can anyone explain this one here ?
This select returns, what I want:
SELECT struc~node,struc~tree_level,struc~parent,
strut~ltext,
mar~matnr, mar~mtart, mar~matkl,
mtx~maktx
FROM wrf_matgrp_struc AS struc
LEFT OUTER JOIN wrf_matgrp_strct AS strut
ON strut~hier_id = struc~hier_id
AND strut~node = struc~node
LEFT OUTER JOIN wrf_matgrp_sku AS sku
ON sku~hier_id = struc~hier_id
AND sku~node = struc~node
LEFT OUTER JOIN mara AS mar
ON mar~matnr = sku~matnr
LEFT OUTER JOIN maw1 AS maw
ON maw~matnr = mar~matnr
LEFT OUTER JOIN makt AS mtx
ON mtx~matnr = mar~matnr
AND mtx~spras = strut~spras
INTO TABLE #lt_result_tab
WHERE struc~hier_id = #gs_act_hier-hier_id
AND struc~node IN #grt_node
AND struc~date_from <= #sy-datum
AND struc~date_to >= #sy-datum
AND strut~spras = #sy-langu
AND mar~matnr IN #grt_matnr
AND mar~matkl IN #grt_matkl
AND maw~wekgr IN #grt_wekgr
I remove the join AND mtx~spras = strut~spras and place it into a where condition like this, and it does not work as expected ( resultset is incomplete ).
SELECT struc~node,struc~tree_level,struc~parent,
strut~ltext,
mar~matnr, mar~mtart, mar~matkl,
mtx~maktx
FROM wrf_matgrp_struc AS struc
LEFT OUTER JOIN wrf_matgrp_strct AS strut
ON strut~hier_id = struc~hier_id
AND strut~node = struc~node
LEFT OUTER JOIN wrf_matgrp_sku AS sku
ON sku~hier_id = struc~hier_id
AND sku~node = struc~node
LEFT OUTER JOIN mara AS mar
ON mar~matnr = sku~matnr
LEFT OUTER JOIN maw1 AS maw
ON maw~matnr = mar~matnr
LEFT OUTER JOIN makt AS mtx
ON mtx~matnr = mar~matnr
INTO TABLE #lt_result_tab
WHERE struc~hier_id = #gs_act_hier-hier_id
AND struc~node IN #grt_node
AND struc~date_from <= #sy-datum
AND struc~date_to >= #sy-datum
AND strut~spras = #sy-langu
AND mar~matnr IN #grt_matnr
AND mar~matkl IN #grt_matkl
AND maw~wekgr IN #grt_wekgr
AND mtx~spras = #sy-langu.
Is it possible to do case statements and to left outer joins like I am doing below? I put comments on what I am trying to achieve.
SELECT
eventGame.Id,
eventGame.[Type] AS GameType
FROM GrassrootsHoops.EventGame eventGame
LEFT OUTER JOIN (SELECT
divisionGameTeamResult.Id,
divisionGameTeamResult.GameId,
COALESCE(divisionPool.Name, divisionTeamPoolPoolBracket.Name) AS DivisionPoolName
FROM GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
LEFT OUTER JOIN
-- CASE eventGame.[Type] = 1 THEN
GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPool
ON divisionPool.Id = divisionTeamPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
ON divisionPool.Id = divisionPoolSettings.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeam
ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team team
ON team.Id = divisionTeam.Id
LEFT OUTER JOIN
-- CASE eventGame.[Type] = 2 THEN
GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamBracket
ON teamBracket.Id = divisionTeamBracket.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamPoolTeamBracket
ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE divisionGameTeamResult.HomeTeam = 1) homeTeam
ON homeTeam.GameId = eventGame.GameId
You can't do case statements with outer joins. I think you can do what you want, though by using a UNION. Here's an example using a simpler schema where we are doing two separate joins based on the type of game.
SELECT
Game.Id,
PoolResult.Score
FROM
Game
JOIN PoolResult
ON PoolResult.GameId = Game.Id
WHERE
Game.GameType = 1
UNION
SELECT
Game.Id,
BracketResult.Score
FROM
Game
JOIN BracketResult
ON BracketResult.GameId = Game.Id
WHERE
Game.GameType = 2
(Here's a working version at SQL Fiddle)
For your query, you just want to write two completely separate queries one for each type of game. Once you have that working for each separate query, just stick a UNION in between them and the results will be concatenated together. Here's your query modified to use a UNION. I don't have an easy way to check it for errors, so there's probably some minor errors
SELECT
divisionGameTeamResult.Id,
eventGame.[Type] AS GameType
FROM
GrassrootsHoops.EventGame eventGame
JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
ON (divisionGameTeamResult.GameId = eventGame.GameId
JOIN GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
JOIN GrassrootsHoops.DivisionPool divisionPool
ON divisionPool.Id = divisionTeamPool.DivisionPoolId
JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
ON divisionPool.Id = divisionPoolSettings.Id
JOIN GrassrootsHoops.DivisionTeam divisionTeam
ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
JOIN GrassrootsHoops.Team team
ON team.Id = divisionTeam.Id
WHERE
divisionGameTeamResult.HomeTeam = 1
AND
eventGame.[Type] = 1
UNION
SELECT
divisionGameTeamResult.Id,
eventGame.[Type] AS GameType
FROM
GrassrootsHoops.EventGame eventGame
JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
ON (divisionGameTeamResult.GameId = eventGame.GameId)
JOIN GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
JOIN GrassrootsHoops.Team teamBracket
ON teamBracket.Id = divisionTeamBracket.Id
JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
JOIN GrassrootsHoops.Team teamPoolTeamBracket
ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE
divisionGameTeamResult.HomeTeam = 1
AND
eventGame.[Type] = 1
I have a sql query like below:
SELECT CPH.CheckPointID,
CPH.ID AS Check_Point_History_ID,
CLCP.SequenceNo AS Sequence,
CP.Code AS Point_Code,
CPV.ID,
TT.Medium AS Description,
[TEXT_TRANSLATION_ANS].[Medium] AS Value,
CPH.Value_ AS Additional_Information,
EMP.NAME AS Checked_By,
CPH.AnsweredOn AS Checked_On
FROM CHECK_LIST_HISTORY CLH
LEFT JOIN CHECK_POINT_HISTORY CPH
ON CLH.ID = CPH.CheckListHistoryID
INNER JOIN (
SELECT CPH2.CheckPointID,
MAX(CPH2.AnsweredOn) AS MaxDate
FROM CHECK_LIST_HISTORY CLH2
LEFT JOIN CHECK_POINT_HISTORY CPH2
ON CLH2.ID = CPH2.CheckListHistoryID
GROUP BY CPH2.CheckPointID
) tm
ON CPH.CheckPointID = tm.CheckPointID
AND CPH.AnsweredOn = tm.MaxDate
LEFT JOIN CHECK_POINT CP
ON CPH.CheckPointID = CP.ID
LEFT JOIN CHECK_POINT_VALUE CPV
ON CPH.CheckPointValueID = CPV.ID
LEFT JOIN TEXT_TRANSLATION TT
ON CP.TextID = TT.TextID
AND TT.LanguageID = LanguageID
LEFT JOIN CHECK_LIST_CHECK_POINT CLCP
ON CP.ID = CLCP.CheckPointID
LEFT JOIN EMPLOYEE EMP
ON CPH.EmployeeID = EMP.ID
LEFT JOIN [TEXT_TRANSLATION] [TEXT_TRANSLATION_ANS]
ON CPV.AnswerTextID = [TEXT_TRANSLATION_ANS].[TextID]
AND [TEXT_TRANSLATION_ANS].[LanguageID] = TT.LanguageID
LEFT JOIN [TEXT_TRANSLATION] [TEXT_TRANSLATION_RES]
ON CPV.ResponseTextID = [TEXT_TRANSLATION_RES].[TextID]
AND [TEXT_TRANSLATION_RES].[LanguageID] = TT.LanguageID
WHERE CLH.WipOrderNo = 304
AND CLH.WipOrderType = 26
AND CLCP.WorkCenter = 'WC03'
AND CLCP.Facility = 'C1P1'
This query should return me two records with maximum date, but it returns nothing. I think the problem is in the INNER JOIN because when the line with the INNER JOIN is commented, the query returns the following table: