如果我有三个查询,那么查询中的行并不完全相同。如何编写一个SQL语句,将所有三个查询及其所有数据行组合成一个最终查询?
我正在使用的查询当前关闭了一些行,我假设因为行不完全相等,并且根据我的理解Access不支持完全外连接。以下是我目前使用的查询:
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;
显然我需要提出一个嵌套SELECT(SELECT中的SELECT)语句。任何人都可以帮我这个吗?
答案 0 :(得分:0)
在所有三个查询中,选择的列,连接和顺序看起来都是相同的。唯一改变的是WHERE子句。所以也许你只需要将这三个条款组合起来......
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
(给或拿一个()或两个)。