Hello,
I have created a duplicates query that shows me exactly what it needs to, now I want to use the results of this query to limit another query, and I cannot get it to work: Here is what I have:
I have created a duplicates query that shows me exactly what it needs to, now I want to use the results of this query to limit another query, and I cannot get it to work: Here is what I have:
SQL:
SELECT
kA.KitInventory_ID, kA.call_ID,kA.Kittype_ID, CD.Call_ID, CD.CASE_ID, KT.Kit_No, KI.LotNo, CT.CallType, CD.SurgeryDate,
HSTA.ShipToAddress_Desc
FROM
HubInv.KitAllocation KA JOIN
HubInv.Call_Details CD on CD.Call_ID = KA.Call_ID INNER JOIN
HubInv.CallType CT on CT.CallType_ID = CD.CallType_ID INNER JOIN
HubInv.Hospital_ShipTo_Address HSTA ON HSTA.Hosp_Shipto_Address_ID = CD.Hosp_Shipto_Address_ID INNER JOIN
HubInv.Kit_Types KT ON KT.KitType_ID = KA.KitType_ID LEFT JOIN
HubInv.KitInventory KI ON KI.KitInventory_ID = KA.KitInventory_ID
WHERE KA.KitInventory_ID IN
(WITH CTE_ALLOCATED
(KitAllocation_ID, KitInventory_ID, RowNumber)
AS
(
SELECT
KitAllocation_ID, KitInventory_ID, ROW_NUMBER() OVER (PARTITION BY KitInventory_ID ORDER BY KitAllocation_ID) AS 'ROW_NUMBER' FROM [HubInv].[KitAllocation]
)
SELECT B.KitAllocation_ID, B.KitInventory_ID, A.KitAllocation_ID AS 'Duplicate Of'
FROM CTE_Allocated B
Inner join CTE_Allocated A
ON B.KitInventory_ID = a.KitInventory_ID
WHERE B.RowNumber > 1
AND A.RowNumber = 1 )