我有一个UNION查询,它结合了大约28个查询。花几个小时是正常的吗?我可以在大约20-30分钟内单独运行它们。下面是我的代码片段。对不起,如果它凌乱。
SELECT DISTINCT AP_CA_WM_Patrol.SID, [HR_Report].[Full_Nm]AS Name, 0 AS [Licensed and Registered (1= Yes; 0= No)], 0 AS [Antitrust (1= Yes; 0= No)], 0 AS [MFP (1= Yes; 0= No)], 0 AS [Doddfrank AP (1= Yes; 0= No)], 0 AS [FX_Trader (1= Yes; 0= No)], 0 AS [FX_Sales (1= Yes; 0= No)], 0 AS [OTA (1= Yes; 0= No)]
FROM AP_CA_WM_Patrol, HR_Report ,AM_L_R,AWM_L_R,APAC_L_R,EMEA_L_R,EMEA_L_R_Certified,LATAM_L_R,Antitrust,MFP,DoddFrank
WHERE (((AP_CA_WM_Patrol.SID)=[HR_Report].[Standard_Id]) AND ((AP_CA_WM_Patrol.[Business Level 1])="ASSET & WEALTH MANAGEMENT")
AND
((AP_CA_WM_Patrol.SID)<>[AM_L_R].[Username])
AND
((AP_CA_WM_Patrol.SID)<>[AWM_L_R].[id_sid])
AND
((AP_CA_WM_Patrol.SID)<>[APAC_L_R].[SID])
AND
((AP_CA_WM_Patrol.SID)<>[EMEA_L_R].[SID])
AND
((AP_CA_WM_Patrol.SID)<>[EMEA_L_R_Certified].[SID])
AND
((AP_CA_WM_Patrol.SID)<>[LATAM_L_R].[UNID])
AND
((AP_CA_WM_Patrol.SID)<>[Antitrust].[SIDs])
AND
((AP_CA_WM_Patrol.SID)<>[MFP].[SID])
AND
((AP_CA_WM_Patrol.SID)<>[Doddfrank].[SID]))
UNION
SELECT DISTINCT AP_CA_WM_SWAP.SID, [HR_Report].[Full_Nm]AS Name, 0 AS [Licensed and Registered (1= Yes; 0= No)], 0 AS [Antitrust (1= Yes; 0= No)], 0 AS [MFP (1= Yes; 0= No)], 0 AS [Doddfrank AP (1= Yes; 0= No)], 0 AS [FX_Trader (1= Yes; 0= No)], 0 AS [FX_Sales (1= Yes; 0= No)], 0 AS [OTA (1= Yes; 0= No)]
FROM AP_CA_WM_SWAP,HR_Report,AM_L_R,AWM_L_R,APAC_L_R,EMEA_L_R,EMEA_L_R_Certified,LATAM_L_R,Antitrust,MFP,DoddFrank
WHERE (((AP_CA_WM_SWAP.SID)=[HR_Report].[Standard_Id]) AND ((AP_CA_WM_SWAP.[Business Level 1])="“Asset" & "Wealth Management”")
And
((AP_CA_WM_SWAP.SID)<>[AM_L_R].[Username])
AND
((AP_CA_WM_SWAP.SID)<>[AWM_L_R].[id_sid])
AND
((AP_CA_WM_SWAP.SID)<>[APAC_L_R].[SID])
AND
((AP_CA_WM_SWAP.SID)<>[EMEA_L_R].[SID])
AND
((AP_CA_WM_SWAP.SID)<>[EMEA_L_R_Certified].[SID])
AND
((AP_CA_WM_SWAP.SID)<>[LATAM_L_R].[UNID])
AND
((AP_CA_WM_SWAP.SID)<>[Antitrust].[SIDs])
AND
((AP_CA_WM_SWAP.SID)<>[MFP].[SID])
AND
((AP_CA_WM_SWAP.SID)<>[Doddfrank].[SID]))
UNION
SELECT DISTINCT APAC_L_R.SID, [HR_Report].[Full_Nm]AS Name, 1 AS [Licensed and Registered (1= Yes; 0= No)], 0 AS [Antitrust (1= Yes; 0= No)], 0 AS [MFP (1= Yes; 0= No)], 0 AS [Doddfrank AP (1= Yes; 0= No)], 0 AS [FX_Trader (1= Yes; 0= No)], 0 AS [FX_Sales (1= Yes; 0= No)], 0 AS [OTA (1= Yes; 0= No)]
FROM APAC_L_R, HR_Report
WHERE (((APAC_L_R.SID)=[HR_Report].[Standard_Id]));
答案 0 :(得分:1)
评论太长了。
union
需要删除重复项,因为它会将结果集合在一起。一般来说,同时使用select distinct
和union all
如果子查询确实没有返回重复项,则将select distinct
替换为select
。
如果您不希望子查询之间出现重复,请将union
更改为union all
。
如果同时执行这两项操作,则查询应该返回得更快。