如何将项添加到Unionized SQL视图?

时间:2012-09-11 01:15:34

标签: sql database select union crystal-reports-xi

我正在使用Crystal Reports制作一个自动化报告,要求我将表格添加到现有的预先编写的视图中。该视图包含一个Union操作,阻止我正常使用该视图。我所做的是手动添加数据(大约一半)。

报告所需的项目是tblSOPartsUsed.Memo。我已添加到BOTH选择查询中。在tblCustomerInventory和tblSOPartsUsed之间的连接中,.Memo表仅出现在PartsUsed表中,而不出现在CustomerInventory中。

  1. 首先选择查询位置

                  tblCustomerInventory.SerialNumber AS ExchangeSerialNumber, **tblSOPartsUsed.Memo AS SOPartsNotes**, tblInvoiceDetail.Taxable AS DetailIsTaxable, 
    
  2. 首先来自查询位置

                  tblCustomerInventory FULL OUTER JOIN
                  tblSOPartsUsed INNER JOIN
    
  3. 我以完全相同的方式概述了第二个Select和From。

    有人可以说清楚,我已经尝试了一切!

    完整代码如下......

    SELECT InvoiceAssemblyPriceBook.Features AS AssemblyFeatures,
        tblInvoiceAssemblyDetail.EachQuantity AS AssemblyEachQuantity,
        tblInvoiceAssemblyDetail.FKInvoiceDetail AS AssemblyFKInvoiceDetail,
        tblInvoiceAssemblyDetail.InvoiceAssemblyDetailKeyID,
        tblInvoiceAssemblyDetail.ItemID AS AssemblyItemID,
        tblInvoiceAssemblyDetail.ItemDescription AS AssemblyItemDescription,
        tblInvoiceAssemblyDetail.PrintOnInvoice AS AssemblyPrintOnInvoice,
        tblInvoiceAssemblyDetail.Quantity AS AssemblyQuantity,
        tblInvoiceAssemblyDetail.SellingPrice AS AssemblySellingPrice,
        tblInvoiceAssemblyDetail.TotalSellingPrice AS AssemblyTotalSellingPrice,
        tblInvoiceAssemblyDetail.Type AS AssemblyType,
        tblInvoiceAssemblyDetail.UnitOfMeasure AS AssemblyUnitOfMeasure,
        tblInvoiceDetail.AssemblyType AS DetailAssemblyType,
        tblInvoiceDetail.InvoiceDetailKeyID,
        tblInvoiceDetail.ItemDescription AS DetailItemDescription,
        tblInvoiceDetail.ItemID AS DetailItemID,
        tblInvoiceDetail.PrintOnInvoice AS DetailPrintOnInvoice,
        tblInvoiceDetail.Quantity AS DetailQuantity,
        tblInvoiceDetail.SellingPrice AS DetailSellingPrice,
        tblInvoiceDetail.TotalSellingPrice AS DetailTotalSellingPrice,
        tblInvoiceDetail.Type AS DetailType,
        tblInvoices.AccountNumber,
        tblInvoices.Comments,
        tblInvoices.ContractNumber,
        tblInvoices.Deposit,
        tblInvoices.Freight,
        tblInvoices.GSTax,
        tblInvoices.InvoiceDate,
        tblInvoices.InvoiceNumber,
        tblInvoices.PaidDate,
        tblInvoices.QuoteNumber,
        tblInvoices.SalesTaxPercent,
        CASE 
            WHEN tblInvoiceDetail.SoNumber IS NULL
                THEN tblInvoices.SONumber
            ELSE tblInvoiceDetail.SoNumber
            END AS SONumber,
        tblInvoices.STATUS,
        tblInvoices.StatusDate,
        tblInvoices.Tax,
        tblInvoices.TotalAmountDue,
        tblInvoices.TotalComment,
        tblInvoices.TotalDollarsDiscounted,
        tblInvoices.TotalGrossSell,
        tblInvoices.TotalNetSell,
        tblInvoices.TradeIn,
        tblInvoices.WorkOrderNumber,
        tblPriceLevels.IsRepairLevel,
        tblServiceOrders.ContractNumber AS ServiceOrderContractNumber,
        tblSysCompanySettings.HideGSTaxRelatedInformation,
        tblSysCompanySettings.ItemsServicedPrintOnInvoice,
        tblSysDisclaimerSettings.SalesInvoiceDisclaimer,
        tblSysDisclaimerSettings.ServiceInvoiceDisclaimer,
        tblSysPBSettings.PrintItemorPartNum,
        VoidedByReps.RepName AS VoidedByRepName,
        tblInvoices.TotalNetInvoice,
        InvoiceDetailPriceBook.PartNumber AS DetailPartNumber,
        InvoiceDetailPriceBook.UnitOfMeasure AS DetailUnitOfMeasure,
        InvoiceDetailPriceBook.Features AS DetailFeatures,
        tblSysDisclaimerSettings.ContractInvoiceDisclaimer,
        tblInvoices.ProviderTax,
        tblServiceOrders.BriefDescription AS SOBriefDescription,
        tblInvoices.GSTaxComputedBeforeTradeIn,
        tblInvoices.TaxComputedBeforeTradeIn,
        tblInvoices.ProviderTaxRate,
        tblInvoices.FreightTaxable,
        tblInvoices.GSTIsTaxable,
        tblInvoices.ProjectKeyID,
        InvoicesReps.RepName,
        tblAccounts.AccountNumber AS Expr1,
        tblAccounts.AccountID,
        tblInvoices.Terms,
        tblInvoices.Reference,
        tblInvoices.ARCustomerNumber,
        tblInvoices.PONumber,
        tblInvoices.ShipVia,
        tblServiceOrders.DateRequested,
        tblServiceOrders.DateOpened,
        tblServiceOrders.SONumber AS ServiceOrderSONumber,
        tblSysReportSettings.InvoiceCommentsAtEnd,
        tblInvoices.SourceDocument,
        tblTaxCodes.HasTieredDistrict,
        tblExchange.ExchangeKeyID,
        tblCustomerInventory.ItemID AS ExchangeItemID,
        tblCustomerInventory.ItemDescription AS ExchangeItemDescription,
        tblCustomerInventory.SerialNumber AS ExchangeSerialNumber,
        tblSOPartsUsed.Memo AS SOPartsNotes,
        tblInvoiceDetail.Taxable AS DetailIsTaxable,
        tblInvoiceAssemblyDetail.Taxable AS AssemblyIsTaxable,
        tblInvoices.IsFinalProgressiveInvoice,
        tblInvoiceDetail.IsProgressiveInvoiceItem,
        tblInvoices.IsProgressiveInvoice,
        tblInvoices.TotalPriceCredited,
        tblInvoices.TotalTaxCredited,
        tblInvoices.TotalGSTaxCredited,
        tblInvoices.TotalProviderTaxCredited,
        tblInvoices.TotalFreightCredited,
        tblInvoices.DiscountAllowed,
        tblInvoices.AmountPaid,
        (
            SELECT MAX(SOItemsServicedKeyID) AS Expr1
            FROM tblSOItemsServiced
            WHERE (SONumber = tblServiceOrders.SONumber)
            ) AS SOItemsServicedKeyID,
        tblInvoiceDetail.CommentOnly,
        ServiceOrderContacts.ContactName AS SOContactName,
        tblServiceOrders.ContactPhone AS SOContactPhone,
        tblServiceOrders.ContactPhoneLocation AS SOContactPhoneLocation,
        (
            SELECT TOP (1) FormattedPhoneNumber
            FROM tblPhoneNumbers
            WHERE (ContactNumber = 0)
                AND (PrimaryIndicator = 1)
                AND (COALESCE(PhoneLocation, '') <> 'Fax')
                AND (AccountNumber = tblAccounts.AccountNumber)
            ) AS AccountPhone,
        (
            SELECT TOP (1) PhoneLocation
            FROM tblPhoneNumbers AS tblPhoneNumbers_1
            WHERE (ContactNumber = 0)
                AND (PrimaryIndicator = 1)
                AND (COALESCE(PhoneLocation, '') <> 'Fax')
                AND (AccountNumber = tblAccounts.AccountNumber)
            ) AS AccountPhoneLocation,
        COALESCE(tvwr_TotalAmountDuePerAccount.AmountDue, 0.00) AS AmountDue,
        COALESCE(tvwr_TotalAmountDuePerAccount.Unappliedpayments, 0.00) AS Unappliedpayments,
        tblTaxCodes.IsHarmonizedTaxCode,
        tblInvoices.GSTax + tblInvoices.Tax AS HSTax
    FROM tblPriceBook AS InvoiceAssemblyPriceBook
    RIGHT JOIN tblPriceBook AS InvoiceDetailPriceBook
    RIGHT JOIN tblInvoiceAssemblyDetail
    RIGHT JOIN tblCustomerInventory
    FULL JOIN tblSOPartsUsed
    INNER JOIN tblExchange
        ON tblSOPartsUsed.SOPartsUsedKeyID = tblExchange.FKSOPartsUsed
            ON tblCustomerInventory.CustomerInventoryKeyID = tblExchange.FKCustomerInventory RIGHT JOIN tblInvoiceDetail
            ON tblSOPartsUsed.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
                ON tblInvoiceAssemblyDetail.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
                    ON InvoiceDetailPriceBook.ItemID = tblInvoiceDetail.ItemID
                        ON InvoiceAssemblyPriceBook.ItemID = tblInvoiceAssemblyDetail.ItemID LEFT JOIN tblPriceLevels
                        ON tblInvoiceDetail.PriceLevel = tblPriceLevels.PriceLevelsKeyID RIGHT JOIN tvwr_TotalAmountDuePerAccount INNER JOIN tblAccounts
                    ON tvwr_TotalAmountDuePerAccount.AccountNumber = tblAccounts.AccountNumber RIGHT JOIN tblReps AS VoidedByReps RIGHT JOIN tblInvoices AS tblInvoices LEFT JOIN tblTaxCodes
            ON tblInvoices.SalesTaxCode = tblTaxCodes.SalesTaxCode LEFT JOIN tblReps AS InvoicesReps
            ON tblInvoices.SalesRep = InvoicesReps.RepNumber
                ON VoidedByReps.RepNumber = tblInvoices.StatusBy LEFT JOIN tblServiceOrders LEFT JOIN tblContacts AS ServiceOrderContacts
            ON tblServiceOrders.ContactNumber = ServiceOrderContacts.ContactNumber
                ON tblInvoices.SONumber = tblServiceOrders.SONumber
                    ON tblAccounts.AccountNumber = tblInvoices.AccountNumber
                        ON tblInvoiceDetail.InvoiceNumber = tblInvoices.InvoiceNumber LEFT JOIN tblContacts AS tblContacts
                        ON tblAccounts.PrimaryContactNumber = tblContacts.ContactNumber CROSS JOIN tblSysPBSettings CROSS JOIN tblSysCompanySettings CROSS JOIN tblSysReportSettings CROSS JOIN tblSysDisclaimerSettings
    WHERE (tblInvoices.MSPAgreementNumber = 0)
    
    UNION ALL
    
    SELECT InvoiceAssemblyPriceBook.Features AS AssemblyFeatures,
        tblInvoiceAssemblyDetail.EachQuantity AS AssemblyEachQuantity,
        tblInvoiceAssemblyDetail.FKInvoiceDetail AS AssemblyFKInvoiceDetail,
        tblInvoiceAssemblyDetail.InvoiceAssemblyDetailKeyID,
        tblInvoiceAssemblyDetail.ItemID AS AssemblyItemID,
        tblInvoiceAssemblyDetail.ItemDescription AS AssemblyItemDescription,
        tblInvoiceAssemblyDetail.PrintOnInvoice AS AssemblyPrintOnInvoice,
        tblInvoiceAssemblyDetail.Quantity AS AssemblyQuantity,
        tblInvoiceAssemblyDetail.SellingPrice AS AssemblySellingPrice,
        tblInvoiceAssemblyDetail.TotalSellingPrice AS AssemblyTotalSellingPrice,
        tblInvoiceAssemblyDetail.Type AS AssemblyType,
        tblInvoiceAssemblyDetail.UnitOfMeasure AS AssemblyUnitOfMeasure,
        tblInvoiceDetail.AssemblyType AS DetailAssemblyType,
        tblInvoiceDetail.InvoiceDetailKeyID,
        tblInvoiceDetail.ItemDescription AS DetailItemDescription,
        tblInvoiceDetail.ItemID AS DetailItemID,
        tblInvoiceDetail.PrintOnInvoice AS DetailPrintOnInvoice,
        tblInvoiceDetail.Quantity AS DetailQuantity,
        tblInvoiceDetail.SellingPrice AS DetailSellingPrice,
        tblInvoiceDetail.TotalSellingPrice AS DetailTotalSellingPrice,
        tblInvoiceDetail.Type AS DetailType,
        tblInvoices.AccountNumber,
        tblInvoices.Comments,
        tblInvoices.ContractNumber,
        tblInvoices.Deposit,
        tblInvoices.Freight,
        tblInvoices.GSTax,
        tblInvoices.InvoiceDate,
        tblInvoices.InvoiceNumber,
        tblInvoices.PaidDate,
        tblInvoices.QuoteNumber,
        tblInvoices.SalesTaxPercent,
        CASE 
            WHEN tblInvoiceDetail.SoNumber IS NULL
                THEN tblInvoices.SONumber
            ELSE tblInvoiceDetail.SoNumber
            END AS SONumber,
        tblInvoices.STATUS,
        tblInvoices.StatusDate,
        tblInvoices.Tax,
        tblInvoices.TotalAmountDue,
        tblInvoices.TotalComment,
        tblInvoices.TotalDollarsDiscounted,
        tblInvoices.TotalGrossSell,
        tblInvoices.TotalNetSell,
        tblInvoices.TradeIn,
        tblInvoices.WorkOrderNumber,
        tblPriceLevels.IsRepairLevel,
        tblServiceOrders.ContractNumber AS ServiceOrderContractNumber,
        tblSysCompanySettings.HideGSTaxRelatedInformation,
        tblSysCompanySettings.ItemsServicedPrintOnInvoice,
        tblSysDisclaimerSettings.SalesInvoiceDisclaimer,
        tblSysDisclaimerSettings.MSPAgreementInvoiceDisclaimer AS ServiceInvoiceDisclaimer,
        tblSysPBSettings.PrintItemorPartNum,
        VoidedByReps.RepName AS VoidedByRepName,
        tblInvoices.TotalNetInvoice,
        InvoiceDetailPriceBook.PartNumber AS DetailPartNumber,
        InvoiceDetailPriceBook.UnitOfMeasure AS DetailUnitOfMeasure,
        InvoiceDetailPriceBook.Features AS DetailFeatures,
        tblSysDisclaimerSettings.ContractInvoiceDisclaimer,
        tblInvoices.ProviderTax,
        tblServiceOrders.BriefDescription AS SOBriefDescription,
        tblInvoices.GSTaxComputedBeforeTradeIn,
        tblInvoices.TaxComputedBeforeTradeIn,
        tblInvoices.ProviderTaxRate,
        tblInvoices.FreightTaxable,
        tblInvoices.GSTIsTaxable,
        tblInvoices.ProjectKeyID,
        InvoicesReps.RepName,
        tblAccounts.AccountNumber AS Expr1,
        tblAccounts.AccountID,
        tblInvoices.Terms,
        tblInvoices.Reference,
        tblInvoices.ARCustomerNumber,
        tblInvoices.PONumber,
        tblInvoices.ShipVia,
        tblServiceOrders.DateRequested,
        tblServiceOrders.DateOpened,
        tblServiceOrders.SONumber AS ServiceOrderSONumber,
        tblSysReportSettings.InvoiceCommentsAtEnd,
        tblInvoices.SourceDocument,
        tblTaxCodes.HasTieredDistrict,
        tblExchange.ExchangeKeyID,
        tblCustomerInventory.ItemID AS ExchangeItemID,
        tblCustomerInventory.ItemDescription AS ExchangeItemDescription,
        tblCustomerInventory.SerialNumber AS ExchangeSerialNumber,
        tblSOPartsUsed.Memo AS SOPartsNotes,
        tblInvoiceDetail.Taxable AS DetailIsTaxable,
        tblInvoiceAssemblyDetail.Taxable AS AssemblyIsTaxable,
        tblInvoices.IsFinalProgressiveInvoice,
        tblInvoiceDetail.IsProgressiveInvoiceItem,
        tblInvoices.IsProgressiveInvoice,
        tblInvoices.TotalPriceCredited,
        tblInvoices.TotalTaxCredited,
        tblInvoices.TotalGSTaxCredited,
        tblInvoices.TotalProviderTaxCredited,
        tblInvoices.TotalFreightCredited,
        tblInvoices.DiscountAllowed,
        tblInvoices.AmountPaid,
        (
            SELECT MAX(SOItemsServicedKeyID) AS Expr1
            FROM tblSOItemsServiced
            WHERE (SONumber = tblServiceOrders.SONumber)
            ) AS SOItemsServicedKeyID,
        tblInvoiceDetail.CommentOnly,
        ServiceOrderContacts.ContactName AS SOContactName,
        tblServiceOrders.ContactPhone AS SOContactPhone,
        tblServiceOrders.ContactPhoneLocation AS SOContactPhoneLocation,
        (
            SELECT TOP (1) FormattedPhoneNumber
            FROM tblPhoneNumbers
            WHERE (ContactNumber = 0)
                AND (PrimaryIndicator = 1)
                AND (COALESCE(PhoneLocation, '') <> 'Fax')
                AND (AccountNumber = tblAccounts.AccountNumber)
            ) AS AccountPhone,
        (
            SELECT TOP (1) PhoneLocation
            FROM tblPhoneNumbers AS tblPhoneNumbers_1
            WHERE (ContactNumber = 0)
                AND (PrimaryIndicator = 1)
                AND (COALESCE(PhoneLocation, '') <> 'Fax')
                AND (AccountNumber = tblAccounts.AccountNumber)
            ) AS AccountPhoneLocation,
        COALESCE(tvwr_TotalAmountDuePerAccount.AmountDue, 0.00) AS AmountDue,
        COALESCE(tvwr_TotalAmountDuePerAccount.Unappliedpayments, 0.00) AS Unappliedpayments,
        COALESCE(tblTaxCodes.IsHarmonizedTaxCode, 0) AS IsHarmonizedTaxCode,
        tblInvoices.GSTax + tblInvoices.Tax AS HSTax
    FROM tblPriceBook AS InvoiceAssemblyPriceBook
    RIGHT JOIN tblPriceBook AS InvoiceDetailPriceBook
    RIGHT JOIN tblInvoiceAssemblyDetail
    RIGHT JOIN tblCustomerInventory
    FULL JOIN tblSOPartsUsed
    INNER JOIN tblExchange
        ON tblSOPartsUsed.SOPartsUsedKeyID = tblExchange.FKSOPartsUsed
            ON tblCustomerInventory.CustomerInventoryKeyID = tblExchange.FKCustomerInventory RIGHT JOIN tblInvoiceDetail
            ON tblSOPartsUsed.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
                ON tblInvoiceAssemblyDetail.FKInvoiceDetail = tblInvoiceDetail.InvoiceDetailKeyID
                    ON InvoiceDetailPriceBook.ItemID = tblInvoiceDetail.ItemID
                        ON InvoiceAssemblyPriceBook.ItemID = tblInvoiceAssemblyDetail.ItemID LEFT JOIN tblPriceLevels
                        ON tblInvoiceDetail.PriceLevel = tblPriceLevels.PriceLevelsKeyID RIGHT JOIN tvwr_TotalAmountDuePerAccount INNER JOIN tblAccounts
                    ON tvwr_TotalAmountDuePerAccount.AccountNumber = tblAccounts.AccountNumber RIGHT JOIN tblReps AS VoidedByReps RIGHT JOIN tblInvoices AS tblInvoices LEFT JOIN tblTaxCodes
            ON tblInvoices.SalesTaxCode = tblTaxCodes.SalesTaxCode LEFT JOIN tblReps AS InvoicesReps
            ON tblInvoices.SalesRep = InvoicesReps.RepNumber
                ON VoidedByReps.RepNumber = tblInvoices.StatusBy LEFT JOIN tblServiceOrders LEFT JOIN tblContacts AS ServiceOrderContacts
            ON tblServiceOrders.ContactNumber = ServiceOrderContacts.ContactNumber
                ON tblInvoices.SONumber = tblServiceOrders.SONumber
                    ON tblAccounts.AccountNumber = tblInvoices.AccountNumber
                        ON tblInvoiceDetail.InvoiceNumber = tblInvoices.InvoiceNumber LEFT JOIN tblContacts AS tblContacts
                        ON tblAccounts.PrimaryContactNumber = tblContacts.ContactNumber CROSS JOIN tblSysPBSettings CROSS JOIN tblSysCompanySettings CROSS JOIN tblSysReportSettings CROSS JOIN tblSysDisclaimerSettings
    WHERE (tblInvoices.MSPAgreementNumber <> 0)
    

1 个答案:

答案 0 :(得分:0)

我设法将视图的每一半复制到一个新视图中,从GUI手动勾选tblSOPartsUsed.memo并将两者复制在一起使视图工作得很好