Создание следующей временной таблицы:
CREATE TABLE #BEDRAGEN(Dossiercode T_Code_Dossier,Detailcode T_Code_Detail,DetailSubcode T_Code_DetailSub
,Totaalbedrag T_Fin_Amount,TotGefactDiensten T_Fin_Amount, TotGefactgoederen T_Fin_Amount, Verzonden int
,TotaalInkord T_Fin_Amount, TotaalInkRgls int,Inkooporder T_Nr_Ord, InkOrdRgl T_LineNR)
INSERT INTO #BEDRAGEN
SELECT DD.DossierCode as Dossiercode,
DD.DetailCode as Detailcode,
DD.DetailSubCode as DetailSubcode,
ISNULL(DBO.SIF_get_SalesAmountDosDetail(DD.Dossiercode,DD.Detailcode,DD.Detailsubcode),0) as Totaalbedrag,
ISNULL((SELECT SUM(ID.InvDetailAmount)
FROM T_InvoiceDetailDosDet as IDD
inner join T_InvoiceDetail as ID on ID.InvCode=IDD.InvCode and ID.InvLineNr=IDD.InvLinenr
WHERE IDD.DossierCode=DD.Dossiercode AND IDD.DetailCode=DD.Detailcode AND IDD.DetailSubCode=DD.DetailSubcode),0)
AS TotGefactDiensten,
ISNULL((SELECT SUM(ID.InvDetailAmount)
from T_InvoiceDetailShipDet as InvSD
inner join T_ShippingDetail as SD on SD.ShipDocCode=InvSD.ShipDocCode and SD.ShipLineNr=InvSD.ShipLineNr
inner join T_InvoiceDetail as ID on ID.InvCode=InvSD.InvCode and ID.InvLineNr=InvSD.InvLinenr
where SD.DossierCode=DD.Dossiercode AND SD.DetailCode=DD.Detailcode AND SD.DetailSubCode=DD.DetailSubcode),0) as TotGefactgoederen,
ISNULL((select sum(Verz.DelQty)
from dbo.T_DeliveryLine as Verz
where Verz.DossierCode=DD.Dossiercode and Verz.DetailCode=DD.Detailcode and Verz.DetailSubCode=DD.DetailSubcode),0) as Verzonden,
ISNULL((select sum(PDPL.BasicCurrTotalAmount)
from T_PurDocPartLineDosDetLink AS PDL
INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
INNER JOIN T_PurDocPartLine as PDPL on PDPL.PurDocCode=PDL.PurDocCode and PDPL.PDPartLineNr=PDL.PDPartLineNr
WHERE PDL.DossierCode=DD.DossierCode AND PDL.DetailCode=DD.DetailCode AND PDL.DetailSubCode=DD.DetailSubCode),0) AS TotaalInkord,
ISNULL((select COUNT(PDL.DetailCode)
from T_PurDocPartLineDosDetLink AS PDL
INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
INNER JOIN T_PurDocPartLine as PDPL on PDPL.PurDocCode=PDL.PurDocCode and PDPL.PDPartLineNr=PDL.PDPartLineNr
WHERE PDL.DossierCode=DD.DossierCode AND PDL.DetailCode=DD.DetailCode AND PDL.DetailSubCode=DD.DetailSubCode),0) AS TotaalInkRgls,
ISNULL((SELECT TOP 1 PD.PurOrdNr FROM T_PurDocPartLineDosDetLink AS PDL
INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
INNER JOIN T_PurDocPartLine AS INR ON INR.PurDocCode=PDL.PurDocCode AND INR.PDPartLineNr=PDL.PDPartLineNr
WHERE PDL.DossierCode=DD.Dossiercode AND PDL.DetailCode=DD.Detailcode AND PDL.DetailSubCode=DD.DetailSubCode),'') AS Inkooporder,
ISNULL((SELECT TOP 1 PDL.PDPartLineNr FROM T_PurDocPartLineDosDetLink AS PDL
INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
INNER JOIN T_PurDocPartLine AS INR ON INR.PurDocCode=PDL.PurDocCode AND INR.PDPartLineNr=PDL.PDPartLineNr
WHERE PDL.DossierCode=DD.Dossiercode AND PDL.DetailCode=DD.Detailcode AND PDL.DetailSubCode=DD.DetailSubCode),0) AS InkOrdRgl
FROM dbo.T_DossierDetail AS DD
Inner Join dbo.T_Part AS P On P.PartCode = DD.PartCode
Inner Join dbo.T_DossierMain AS DM On DM.DossierCode = DD.DossierCode
Inner Join dbo.T_DossierStatus AS DS On DS.DossierStatusCode = DD.DossierStatusCode
WHERE DD.DossierCode > N''
select * from #bedragen
drop table #BEDRAGEN
Как вы можете видеть подзапросы для получения столбцов TotaalInkord, TotaalInkRgls, Inkooporder, InkOrdRgl
используют то же самое из / innerjoin и где.
Есть ли возможность объединить их?
Если объединение возможно, оно будет быстрее?
Спасибо
Справочная информация:
Мне нужна эта временная таблица для ускорения производительности хранимой процедуры.
Эта хранимая процедура представляет собой большой выбор, использующий ряд функций для получения своих столбцов.
Эту функцию-контент я сейчас добавил в temptable.