将12个MS Access查询与不同的“where”条件组合到一个查询中
我有十二个不同的SELECT查询,每个都有不同的WHERE标准。 每个查询都有相同的ID代码(APINumber),我想创build一个输出:
AP号码| 来自查询1的数据| 来自查询2的数据| 等等
我目前有每个查询链接到Excel,并刷新Excel中的每个查询后,我有一个单独的选项卡的Vlookups结合了所有的数据。 我的问题是查询需要很长时间才能刷新,所以通过excel进行合并是非常低效的。 最终,我的目标是链接到Tableau文件以进行可视化。
我发现如何链接2个查询的留言板,但我已经不知所措了试图链接所有12个。对于给定的ID代码,一些数据字段将是空白的,但是我仍然希望显示ID代码。
https://dba.stackexchange.com/questions/63506/merge-two-select-queries-with-different-where-clauses
这是我的疑问:
总水量
SELECT dbo_RegistryUpload.APINumber, Avg(dbo_RegistryUpload.TotalWater) AS AvgOfTotalWater FROM (dbo_RegistryUpload AS dbo_RegistryUpload INNER JOIN (dbo_RegistryUploadPurpose INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN [API Count] ON dbo_RegistryUpload.APINumber = [API Count].APINumber WHERE ((([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
水 %
SELECT dbo_RegistryUpload_1.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS WaterPerc FROM (dbo_RegistryUpload AS dbo_RegistryUpload_1 INNER JOIN (dbo_RegistryUploadPurpose INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON dbo_RegistryUpload_1.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN [API Count] ON dbo_RegistryUpload_1.APINumber = [API Count].APINumber WHERE (((dbo_RegistryUploadIngredients.IngredientName) Like "*base*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadIngredients.IngredientName) Like "*Carrier*") AND (([API Count].Howmany)=1)) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*base*")) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*carrier*")) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*water*")) GROUP BY dbo_RegistryUpload_1.APINumber HAVING (((dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*"));
支撑剂
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS Prop FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE (((dbo_RegistryUploadPurpose.Purpose) Like "*prop*" And (dbo_RegistryUploadPurpose.Purpose) Not Like "*GEL*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadPurpose.Purpose) Like "*sand*" And (dbo_RegistryUploadPurpose.Purpose) Not Like "*gel*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber)="42"));
100目支撑剂
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS 100mesh FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE (((dbo_RegistryUploadPurpose.TradeName) Like "*100 mesh*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*"));
20/40支撑剂
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS 2040 FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE (((dbo_RegistryUploadPurpose.TradeName) Like "*20/40*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadPurpose.TradeName) Like "*20-40*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
30/50支撑剂
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS 3050 FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE (((dbo_RegistryUploadPurpose.TradeName) Like "*30/50*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadPurpose.TradeName) Like "*30-50*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
40/70支撑剂
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS 4070 FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE (((dbo_RegistryUploadPurpose.TradeName) Like "*40/70*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadPurpose.TradeName) Like "*40-70*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
断路器%
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS BreakerPerc FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*breaker*")) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
交叉链接质量%
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS Crosslink FROM ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) INNER JOIN [API Count] ON dbo_RegistryUpload.APINumber = [API Count].APINumber WHERE (((dbo_RegistryUploadPurpose.Purpose) Like "cross*" And (dbo_RegistryUploadPurpose.Purpose) Not Like "*breaker*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
凝胶质量
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS GelMass FROM ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) INNER JOIN [API Count] ON dbo_RegistryUpload.APINumber = [API Count].APINumber WHERE (((dbo_RegistryUploadPurpose.Purpose) Like "gel*" And (dbo_RegistryUploadPurpose.Purpose) Not Like "*breaker*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadPurpose.Purpose) Like "*guar*" And (dbo_RegistryUploadPurpose.Purpose) Not Like "*breaker*") AND (([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*"));
树脂
SELECT dbo_RegistryUpload.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS Resin FROM [API Count] INNER JOIN ((dbo_RegistryUpload INNER JOIN dbo_RegistryUploadPurpose ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON [API Count].APINumber = dbo_RegistryUpload.APINumber WHERE (((dbo_RegistryUploadPurpose.Purpose) Like "*resin*") AND (([API Count].Howmany)=1)) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.TradeName) Like "*RCS*")) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*"));
既然你已经有了所有的查询,为什么不把它们用作子查询并把它们连接在一起呢?
select dbo_RegistryUpload.APINumber, AvgOfTotalWater, WaterPerc from (dbo_RegistryUpload as dbo_RegistryUpload left join (SELECT dbo_RegistryUpload.APINumber, Avg(dbo_RegistryUpload.TotalWater) AS AvgOfTotalWater FROM (dbo_RegistryUpload AS dbo_RegistryUpload INNER JOIN (dbo_RegistryUploadPurpose INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON dbo_RegistryUpload.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN [API Count] ON dbo_RegistryUpload.APINumber = [API Count].APINumber WHERE ((([API Count].Howmany)=1)) GROUP BY dbo_RegistryUpload.APINumber HAVING (((dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*" Or (dbo_RegistryUpload.APINumber) Like "42*")) ) as atw on atw.APINumber = dbo_RegistryUpload.APINumber) left join (SELECT dbo_RegistryUpload_1.APINumber, Sum(dbo_RegistryUploadIngredients.PercentHFJob) AS WaterPerc FROM (dbo_RegistryUpload AS dbo_RegistryUpload_1 INNER JOIN (dbo_RegistryUploadPurpose INNER JOIN dbo_RegistryUploadIngredients ON dbo_RegistryUploadPurpose.pKey = dbo_RegistryUploadIngredients.pKeyPurpose) ON dbo_RegistryUpload_1.pKey = dbo_RegistryUploadPurpose.pKeyRegistryUpload) INNER JOIN [API Count] ON dbo_RegistryUpload_1.APINumber = [API Count].APINumber WHERE (((dbo_RegistryUploadIngredients.IngredientName) Like "*base*") AND (([API Count].Howmany)=1)) OR (((dbo_RegistryUploadIngredients.IngredientName) Like "*Carrier*") AND (([API Count].Howmany)=1)) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*base*")) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*carrier*")) OR ((([API Count].Howmany)=1) AND ((dbo_RegistryUploadPurpose.Purpose) Like "*water*")) GROUP BY dbo_RegistryUpload_1.APINumber HAVING (((dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*" Or (dbo_RegistryUpload_1.APINumber) Like "42*")) ) as wp on wp.APINumber = dbo_RegistryUpload.APINumber
希望得到的重点,只是做其余的查询相同的事情。