将 SQL 查询与不同的 where 子句组合

Posted

技术标签:

【中文标题】将 SQL 查询与不同的 where 子句组合【英文标题】:Combine SQL query with different where clause 【发布时间】:2018-06-26 19:50:12 【问题描述】:

我有 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】:

要做到这一点,您只需要一个选择 这是因为你唯一的区别是 R.Channel

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 R.Channel = M.[CHANNEL 2]
 and R.Channel = M.[CHANNEL 3]
 and A.MONTH_END_DATE_KEY = R.MONTH_END_DATE_KEY
 AND  A.RDTYPE is  NULL

【讨论】:

以上是关于将 SQL 查询与不同的 where 子句组合的主要内容,如果未能解决你的问题,请参考以下文章

SQL/Impala:将多个查询(具有不同的 where 子句)合并为一个

SQL Server:如何将 UNION 与两个都有 WHERE 子句的查询一起使用?

仅使用 WHERE 子句组合 LIKE 和 IN

将原生 SQL where 子句应用于实体的 Nhibernate 查询

Laravel 雄辩的 SQL 查询与 OR 和 AND 与 where 子句

SQL WHERE 子句将值与尾随空格匹配