/*
DROP TABLE #TEMPBASEProducts
DROP TABLE #TEMPProducts
DROP TABLE #Result
*/
/--------------------1- fetch all the sales order details with the product------------------------/
SELECT sod.salesorderdetailid
,sod.bcrm_ProductLineName OrderReferenceNumber
,sod.dot_BespokeOfferingName OfferingName
,sod.dot_BaseProductGuidID baseProductGUID
INTO #TEMPProducts
FROM salesorderdetail sod (nolock)
INNER JOIN salesorderdetail sodbase (nolock)
ON sodbase.salesorderdetailid = sod.dot_baseproductguidid
INNER JOIN Product p on p.ProductId = sod.ProductId
WHERE p.ProductNumber IN (‘MKTGS1001BIZULTIMATE’)
AND sod.dot_baseproductguidid IS NOT NULL
–select * from #TEMPProducts
/-------------------- fetch all the sales order details with the product------------------------/
/--------------------2 fetch all the base sales order details------------------------/
SELECT sod.salesorderdetailid
,sod.bcrm_ProductLineName
,sod.dot_BespokeOfferingName OfferingName
into #TEMPBASEProducts
FROM salesorderdetail sod (nolock)
WHERE sod.SalesOrderDetailId in (select baseProductGUID from #TEMPProducts)
–select * from #TEMPBASEProducts
/--------------------3 RESULT------------------------/
SELECT
–sod.salesorderdetailid
sod.dot_CustomerName
,sod.SalesOrderIdName
,sod.bcrm_ProductLineName OrderReferenceNumber
,sod.dot_BespokeOfferingName OfferingName
,sod.dot_DevicesReserved DevicesReceived
,[ProductName]=(select name from Product(nolock) where sod.ProductId = Product.ProductId)
into #Result
FROM salesorderdetail sod (nolock)
WHERE
(sod.SalesOrderDetailId in (select salesorderdetailid from #TEMPProducts))
OR
(sod.SalesOrderDetailId in (select salesorderdetailid from #TEMPBASEProducts))
OR
(sod.dot_BaseProductGuidID in (select salesorderdetailid from #TEMPBASEProducts))
order by sod.bcrm_ProductLineName asc
select * from #Result