views:

93

answers:

1

If I have three queries and rows from the queries are not all alike. How can I write an SQL statement that will combine all three queries and all their rows of data into one final query??

The query I'm using currently leaves some of the rows off, I'm assuming because the rows are not all equal, and from my understanding Access does not support full outer joins. Below is the query that I'm currently using:

SELECT
    ZBASED.ACCT_UNIT,
    CCtable.CenterName,
    ZBASED.ACCOUNT,
    ZBASED.ACCOUNT_DESC,
    qryBUD11.Bud11,
    qryBUD10.Bud10,
    qryACTvPROJ.ActvProj10
FROM
    CCtable,
    (
        (ZBASED INNER JOIN qryACTvPROJ ON ZBASED.ACCOUNT=qryACTvPROJ.ACCOUNT)
        INNER JOIN qryBUD11 ON ZBASED.ACCOUNT=qryBUD11.ACCOUNT
    )
INNER JOIN qryBUD10 ON
    ZBASED.ACCOUNT=qryBUD10.ACCOUNT
WHERE
    ZBASED.ACCT_UNIT = [Cost Center] AND
    CCtable.CenterNo = [Cost Center]
ORDER BY ZBASED.ACCOUNT;

qryACTvPROJ

SELECT BUDGET.FISCAL_YEAR, BUDGET.BUDGET_NBR, BUDGET.ACCT_UNIT, BUDGET.ACCOUNT, ZBASED.ACCOUNT_DESC, BUDGET.DB_AMOUNT_01+BUDGET.DB_AMOUNT_02+BUDGET.DB_AMOUNT_03+BUDGET.DB_AMOUNT_04+BUDGET.DB_ AMOUNT_05+BUDGET.DB_AMOUNT_06+BUDGET.DB_AMOUNT_07+BUDGET.DB_AMOUNT_08+BUDGET.DB_AMOUNT_09+BUDGET.DB_AMOUNT_10+BUDGET.DB_AMOUNT_11+BUDGET.DB_AMOUNT_12+BUDGET.CR_AMOUNT_01+BUDGET.CR_AMOUNT_02+BUDGET.CR_AMOUNT_03+BUDGET.CR_AMOUNT_04+BUDGET.CR_AMOUNT_05+BUDGET.CR_AMOUNT_06+BUDGET.CR_AMOUNT_07+BUDGET.CR_AMOUNT_08+BUDGET.CR_AMOUNT_09+BUDGET.CR_AMOUNT_10+BUDGET.CR_AMOUNT_11+BUDGET.CR_AMOUNT_12 AS ActvProj10
FROM BUDGET INNER JOIN ZBASED ON BUDGET.ACCOUNT=ZBASED.ACCOUNT
WHERE (((BUDGET.FISCAL_YEAR)=2010) And ((BUDGET.BUDGET_NBR)=6) And ((ZBASED.ACCT_UNIT)=BUDGET.ACCT_UNIT And (ZBASED.ACCT_UNIT)=[Cost Center]))
ORDER BY ZBASED.ACCOUNT;

qryBUD11

SELECT BUDGET.FISCAL_YEAR, BUDGET.BUDGET_NBR, BUDGET.ACCT_UNIT, BUDGET.ACCOUNT, ZBASED.ACCOUNT_DESC, BUDGET.DB_AMOUNT_01+BUDGET.DB_AMOUNT_02+BUDGET.DB_AMOUNT_03+BUDGET.DB_AMOUNT_04+BUDGET.DB_AMOUNT_05+BUDGET.DB_AMOUNT_06+BUDGET.DB_AMOUNT_07+BUDGET.DB_AMOUNT_08+BUDGET.DB_AMOUNT_09+BUDGET.DB_AMOUNT_10+BUDGET.DB_AMOUNT_11+BUDGET.DB_AMOUNT_12+BUDGET.CR_AMOUNT_01+BUDGET.CR_AMOUNT_02+BUDGET.CR_AMOUNT_03+BUDGET.CR_AMOUNT_04+BUDGET.CR_AMOUNT_05+BUDGET.CR_AMOUNT_06+BUDGET.CR_AMOUNT_07+BUDGET.CR_AMOUNT_08+BUDGET.CR_AMOUNT_09+BUDGET.CR_AMOUNT_10+BUDGET.CR_AMOUNT_11+BUDGET.CR_AMOUNT_12 AS Bud11
FROM BUDGET INNER JOIN ZBASED ON BUDGET.ACCOUNT = ZBASED.ACCOUNT
WHERE (((BUDGET.FISCAL_YEAR)=2011) AND ((BUDGET.BUDGET_NBR)=2) AND ((ZBASED.ACCT_UNIT)=[BUDGET].[ACCT_UNIT] And (ZBASED.ACCT_UNIT)=[Cost Center]))
ORDER BY ZBASED.ACCOUNT;

qryBUD10

SELECT BUDGET.FISCAL_YEAR, BUDGET.BUDGET_NBR, BUDGET.ACCT_UNIT, BUDGET.ACCOUNT, ZBASED.ACCOUNT_DESC, BUDGET.DB_AMOUNT_01+BUDGET.DB_AMOUNT_02+BUDGET.DB_AMOUNT_03+BUDGET.DB_AMOUNT_04+BUDGET.DB_AMOUNT_05+BUDGET.DB_AMOUNT_06+BUDGET.DB_AMOUNT_07+BUDGET.DB_AMOUNT_08+BUDGET.DB_AMOUNT_09+BUDGET.DB_AMOUNT_10+BUDGET.DB_AMOUNT_11+BUDGET.DB_AMOUNT_12+BUDGET.CR_AMOUNT_01+BUDGET.CR_AMOUNT_02+BUDGET.CR_AMOUNT_03+BUDGET.CR_AMOUNT_04+BUDGET.CR_AMOUNT_05+BUDGET.CR_AMOUNT_06+BUDGET.CR_AMOUNT_07+BUDGET.CR_AMOUNT_08+BUDGET.CR_AMOUNT_09+BUDGET.CR_AMOUNT_10+BUDGET.CR_AMOUNT_11+BUDGET.CR_AMOUNT_12 AS Bud10
FROM BUDGET INNER JOIN ZBASED ON BUDGET.ACCOUNT=ZBASED.ACCOUNT
WHERE (((BUDGET.FISCAL_YEAR)=2010) AND ((BUDGET.BUDGET_NBR)=1) AND ((ZBASED.ACCT_UNIT)=BUDGET.ACCT_UNIT) And ((ZBASED.ACCT_UNIT)=[Cost Center]))
ORDER BY ZBASED.ACCOUNT;

Apparently I need to come up with a Nested SELECT ( SELECT within SELECT) statement. Can anyone help me with this?

A: 

It looks like the columns selected, the join and the order by are the same in all three queries. The only thing that changes is the WHERE clause. So perhaps you just need to OR the three clauses together...

SELECT ...list of columns...
    FROM BUDGET INNER JOIN ZBASED ON BUDGET.ACCOUNT=ZBASED.ACCOUNT
WHERE (((BUDGET.FISCAL_YEAR)=2010) And ((BUDGET.BUDGET_NBR)=6) And ((ZBASED.ACCT_UNIT)=BUDGET.ACCT_UNIT And (ZBASED.ACCT_UNIT)=[Cost Center]))
OR (((BUDGET.FISCAL_YEAR)=2011) AND ((BUDGET.BUDGET_NBR)=2) AND ((ZBASED.ACCT_UNIT)=[BUDGET].[ACCT_UNIT] And (ZBASED.ACCT_UNIT)=[Cost Center]))
OR (((BUDGET.FISCAL_YEAR)=2010) AND ((BUDGET.BUDGET_NBR)=1) AND ((ZBASED.ACCT_UNIT)=BUDGET.ACCT_UNIT) And ((ZBASED.ACCT_UNIT)=[Cost Center]))
ORDER BY ZBASED.ACCOUNT

(give or take a () or two).

Brian Hooper
@ Brian. That worked to bring all the accounts together in my CCquery, but it omitted some of the other information I needed within the query. I have a question for you, can you convert the query to a nested select or select within a select? If so, how would you write it?
Edmond
I'm not sure that a nested select is the solution to your problem; what information is missing in the query? When we have sorted out what is wanted, then we can worry about the means of getting it.
Brian Hooper
I have 16 rows in qrybud11, 15 rows in qrybud10, and 16 rows in qryactvproj. When I use the CCquery to combine all the separate queries together I only get 13 rows of data, because if the queries dont have the same row information it omits the whole row. I need it to import all the rows from all the queries and just add a 0 if one query doesnt have the identical information.
Edmond
I think I can see what you are driving at...you are joining the results of these three queries to form a sort-of-table, and in doing so, some of the rows appear to be vanishing. I think that is an effect of the inner join; you'll only get a result if the ACCOUNT appears in all three queries. I'd suggest doing a FULL OUTER JOIN. Inspect the results (16 * 15 * 16 rows so not too horrendous), and then see what join conditions are necessary to get the rows you need.
Brian Hooper
Ive heard tha Access does not support Full Outer Joins. Is that inaccurate? I've also tried the full outer join and came up with a join error. Not sure exactly what it said but it would not let me run the query.
Edmond
Oh. Perhaps you could create the three queries as views, and then trySELECT * FROM VIEW1, VIEW2, VIEW3; That's an outer join really. I hope I'm not putting you to a lot of trouble when I'm barking up the wrong tree.
Brian Hooper
Not at all just trying to get this up and running properly. How do you create queries as views?? Without giving me the exact way to set the query up, I was told that I needed to try a nested select Select within a select....
Edmond