E Parker E Parker - 3 months ago 7
SQL Question

Combining 12 MS Access queries with different "where" criteria into one query

I have twelve different SELECT queries, each with different WHERE criteria. Each query has the same ID code (APINumber), and I'd like to create an output of:

APINumber | Data from Query 1 | Data from Query 2 | etc

I currently have each query linked to excel, and after refreshing each query in excel, I have a separate tab of vlookups that combines all of the data. My issue is that the queries take a very long time to refresh, so going through excel to then be combined is very inefficient. Ultimately, my goal is to link to a Tableau file for visualizations.

I've found message boards of how to link 2 queries, but I've overwhelmed with trying to link all 12. Some of the data fields will be blank for a given ID code, but I still want the ID code to show.

http://dba.stackexchange.com/questions/63506/merge-two-select-queries-with-different-where-clauses

Here are my queries:

Total Water

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*"));


Water %

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*"));


Proppant

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 Mesh Proppant

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 Proppant

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 Proppant

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 Proppant

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*"));


Gel Mass

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*"));


Resin

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*"));

Answer

Since you already have all the queries written, why not use them as subqueries and join them together?

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

Hopefully that gets the point across, just do the same thing for the rest of your queries.