我有12个不同的SELECT查询,每个查询都有不同的WHERE标准。每个查询都有相同的ID代码(APINumber),我想创建一个输出:
APINumber |查询1中的数据来自查询2的数据|等
我目前将每个查询链接到excel,并在刷新excel中的每个查询后,我有一个单独的vlookup选项卡,它结合了所有数据。我的问题是查询需要很长时间才能刷新,因此通过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*"));
Breaker%
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*"));
CrossLink Mass%
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*"));
答案 0 :(得分:1)
由于您已经编写了所有查询,为什么不将它们用作子查询并将它们连接在一起?
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
希望这可以解决问题,为其余的查询做同样的事情。