将SQL查询与其他where子句结合

时间:2018-06-26 19:50:13

标签: sql performance tsql

我有3个步骤来生成一个输出表,其中第一个表的输出被输入到第二个表,依此类推。唯一的区别是3个查询中的where条件子句。 希望结合起来以提高速度和性能。您能否建议这样做。以下是查询:

SELECT DISTINCT
A.*
,R.Channel
,R.[Deal Description]
into Intermediate_Temp_Table1
--select count(*)
FROM Temp_Table
left join [ID vs CHANNEL_MAPPING] M ON M.Outlet_Id = A.VIP_OUTLET_ID
LEFT JOIN [product TO SUBBRAND MAPPING](nolock) B on A.BPP_CODE = B.[BPP Code] 
LEFT JOIN [DBO].[A1 VS A2] C (nolock) ON (C.[C6 CODE] = A.SHIP_TO_CUST_CODE) 
LEFT JOIN Price_Structure_FY18 R 
 ON B.[SUBBRAND ID]=R.[SUBBRAND  CODE] 
 AND C.[C5 CODE] = R.[CUSTOMER C5  CODE]
 AND CONVERT(decimal(16,2),A.NET_UNIT_PRICE) = CONVERT(decimal(16,2),R.[Suggested PTR])
 and R.Channel = M.[CHANNEL 1]
 and A.MONTH_END_DATE_KEY = R.MONTH_END_DATE_KEY
 AND  A.RDTYPE is  NULL

------------------------------------
  SELECT DISTINCT
A.*
,R.Channel
,R.[Deal Description]
into Intermediate_Temp_Table2
--select count(*)
FROM Intermediate_Temp_Table1
left join [ID vs CHANNEL_MAPPING] M ON M.Outlet_Id = A.VIP_OUTLET_ID
LEFT JOIN [product TO SUBBRAND MAPPING](nolock) B on A.BPP_CODE = B.[BPP Code] 
LEFT JOIN [DBO].[A1 VS A2] C (nolock) ON (C.[C6 CODE] = A.SHIP_TO_CUST_CODE) 
LEFT JOIN Price_Structure_FY18 R 
 ON B.[SUBBRAND ID]=R.[SUBBRAND  CODE] 
 AND C.[C5 CODE] = R.[CUSTOMER C5  CODE]
 AND CONVERT(decimal(16,2),A.NET_UNIT_PRICE) = CONVERT(decimal(16,2),R.[Suggested PTR])
 and R.Channel = M.[CHANNEL 2]
 and A.MONTH_END_DATE_KEY = R.MONTH_END_DATE_KEY
 AND  A.RDTYPE is  NULL

----------------------------------------------------------------------

SELECT DISTINCT
A.*
,R.Channel
,R.[Deal Description]
into Intermediate_Temp_Table3
--select count(*)
FROM Intermediate_Temp_Table2
left join [ID vs CHANNEL_MAPPING] M ON M.Outlet_Id = A.VIP_OUTLET_ID
LEFT JOIN [product TO SUBBRAND MAPPING](nolock) B on A.BPP_CODE = B.[BPP Code] 
LEFT JOIN [DBO].[A1 VS A2] C (nolock) ON (C.[C6 CODE] = A.SHIP_TO_CUST_CODE) 
LEFT JOIN Price_Structure_FY18 R 
 ON B.[SUBBRAND ID]=R.[SUBBRAND  CODE] 
 AND C.[C5 CODE] = R.[CUSTOMER C5  CODE]
 AND CONVERT(decimal(16,2),A.NET_UNIT_PRICE) = CONVERT(decimal(16,2),R.[Suggested PTR])
 and R.Channel = M.[CHANNEL 3]
 and A.MONTH_END_DATE_KEY = R.MONTH_END_DATE_KEY
 AND  A.RDTYPE is  NULL

1 个答案:

答案 0 :(得分:0)

要完成此操作,您只需选择一个 这是因为您唯一的区别是R.Channel

function TForm2.PrintOutLine(sLine: string): boolean;
var
  i: integer;
begin
  i:= 1;
  while (i<=Length(sLine)) do begin
    Memo1.SelStart := Memo1.GetTextLen;
    Memo1.SelText := sLine[i];
    Application.ProcessMessages;
    Sleep(80);
    inc(i);
  end;
  Memo1.SelStart := Memo1.GetTextLen;
  Memo1.SelText := #13#10;
  Result:= True;
end;