访问UNION查询崩溃访问

时间:2017-08-22 02:15:14

标签: sql ms-access join union

我有一个结算系统,可以生成2组发票查询(qInvoicesAutomatic和qInvoicesManual)。我从两个发票查询的联盟构建了两个查询,一个计算发票记录的数量,另一个计算整个账单的总数。我可以成功运行(返回相同数量的记录),每个代码如下:

qUniqueInvoices

SELECT DISTINCT qInvoicesAutomatic.APN AS APN
    ,qInvoicesAutomatic.PeriodID AS PeriodID
    ,count(*) AS InvoiceRecords
    ,(qInvoicesAutomatic.APN & qInvoicesAutomatic.PeriodID) AS PrimKey
FROM qInvoicesAutomatic
GROUP BY qInvoicesAutomatic.APN
    ,qInvoicesAutomatic.PeriodID
    ,(qInvoicesAutomatic.APN & qInvoicesAutomatic.PeriodID)

UNION

SELECT DISTINCT qInvoicesManual.APN AS APN
    ,qInvoicesManual.PeriodID AS PeriodID
    ,count(*) AS InvoiceRecords
    ,(APN & PeriodID) AS PrimKey
FROM qInvoicesManual
GROUP BY qInvoicesManual.APN
    ,qInvoicesManual.PeriodID
    ,(APN & PeriodID);

qUniqueTotals

SELECT DISTINCT qInvoiceBasechargeAUTO.APN
    ,qInvoiceBasechargeAUTO.PeriodID
    ,Sum(qInvoiceBasechargeAUTO.TotalBaseCharge) AS TotalAnnualBaseCharge
    ,qInvoiceUsageAUTO.TotalVariableCharge
    ,(Sum(qInvoiceBasechargeAUTO.TotalBaseCharge) + (qInvoiceUsageAUTO.TotalVariableCharge) - (qinvoiceUsageAUTO.CAREUsageDiscount) - Sum(qInvoiceBasechargeAUTO.CAREBasechargeDiscount)) AS TotalAnnual
    ,qinvoiceUsageAUTO.CAREUsageDiscount
    ,Sum(qInvoiceBasechargeAUTO.CAREBasechargeDiscount) AS TotalCAREBaseChargeDiscount
    ,(qInvoiceBasechargeAUTO.APN & qInvoiceBasechargeAUTO.PeriodID) AS PrimKey
FROM qInvoiceBasechargeAUTO
INNER JOIN qInvoiceUsageAUTO ON qInvoiceBasechargeAUTO.APN = qInvoiceUsageAUTO.APN
WHERE (((qInvoiceBasechargeAUTO.PeriodID) = [qInvoiceUsageAUTO].[PeriodID]))
GROUP BY qInvoiceBasechargeAUTO.APN
    ,qInvoiceBasechargeAUTO.PeriodID
    ,qInvoiceUsageAUTO.TotalVariableCharge
    ,qInvoiceUsageAUTO.CAREUsageDiscount
    ,(qInvoiceBasechargeAUTO.APN & qInvoiceBasechargeAUTO.PeriodID)

UNION

SELECT DISTINCT APN
    ,PeriodID
    ,Sum(TotalBaseCharge) AS TotalAnnualBaseCharge
    ,TotalVariableCharge
    ,(Sum(TotalBaseCharge) + TotalVariableCharge - CAREUsageDiscount - Sum(CAREBaseChargeDiscount)) AS TotalAnnual
    ,(APN & PeriodID) AS PrimKey
    ,CAREUsageDiscount
    ,Sum(CAREBaseChargeDiscount) AS TotalCAREBaseChargeDiscount
FROM qInvoicesManual
GROUP BY APN
    ,Periodid
    ,TotalVariableCharge
    ,CAREUsageDiscount
    ,(APN & PeriodID);

然后我在qUniqueBills中的PrimKey上加入qUniqueInvoices和qUniqueTotals,以获取有关每个账单的信息(发票记录数和总信息)。查询如下:

qUniqueBills

SELECT qUniqueInvoices.APN
    ,qUniqueInvoices.PeriodID
    ,qUniqueInvoices.InvoiceRecords
    ,qUniqueTotals.TotalAnnual
FROM qUniqueInvoices
LEFT JOIN qUniqueTotals ON qUniqueInvoices.PrimKey = qUniqueTotals.PrimKey
ORDER BY qUniqueInvoices.APN
    ,qUniqueInvoices.PeriodID;

我的问题是,当我尝试在数据表格式中过滤qUniqueBills时,它会崩溃Access,或者返回联合查询中存在不等量的行,或者说输入有效值。重申一下,以上所有查询在运行时都是单独运行的。我试过修复数据库。有没有更好的方法来加入包含相同数据集的发票数据?

1 个答案:

答案 0 :(得分:1)

我认为你在第二个查询(quniqeTotals)中的问题必须匹配union中两个select中的字段。 所以权利是

SELECT DISTINCT qInvoiceBasechargeAUTO.APN
,qInvoiceBasechargeAUTO.PeriodID
,Sum(qInvoiceBasechargeAUTO.TotalBaseCharge) AS TotalAnnualBaseCharge
,qInvoiceUsageAUTO.TotalVariableCharge
,(Sum(qInvoiceBasechargeAUTO.TotalBaseCharge) + (qInvoiceUsageAUTO.TotalVariableCharge) - (qinvoiceUsageAUTO.CAREUsageDiscount) - Sum(qInvoiceBasechargeAUTO.CAREBasechargeDiscount)) AS TotalAnnual
,qinvoiceUsageAUTO.CAREUsageDiscount
,Sum(qInvoiceBasechargeAUTO.CAREBasechargeDiscount) AS TotalCAREBaseChargeDiscount
,(qInvoiceBasechargeAUTO.APN & qInvoiceBasechargeAUTO.PeriodID) AS PrimKey
FROM qInvoiceBasechargeAUTO
INNER JOIN qInvoiceUsageAUTO ON qInvoiceBasechargeAUTO.APN = qInvoiceUsageAUTO.APN
WHERE (((qInvoiceBasechargeAUTO.PeriodID) = [qInvoiceUsageAUTO].[PeriodID]))
GROUP BY qInvoiceBasechargeAUTO.APN
,qInvoiceBasechargeAUTO.PeriodID
,qInvoiceUsageAUTO.TotalVariableCharge
,qInvoiceUsageAUTO.CAREUsageDiscount
,(qInvoiceBasechargeAUTO.APN & qInvoiceBasechargeAUTO.PeriodID)

UNION

SELECT DISTINCT APN
,PeriodID
,Sum(TotalBaseCharge) AS TotalAnnualBaseCharge
,TotalVariableCharge
,(Sum(TotalBaseCharge) + TotalVariableCharge - CAREUsageDiscount - Sum(CAREBaseChargeDiscount)) AS TotalAnnual
,CAREUsageDiscount
,Sum(CAREBaseChargeDiscount) AS TotalCAREBaseChargeDiscount
 ,(APN & PeriodID) AS PrimKey
FROM qInvoicesManual
GROUP BY APN
,Periodid
,TotalVariableCharge
,CAREUsageDiscount
,(APN & PeriodID);