1

在 Visual Foxpro 9 中,我试图编写一个带有产品“小计”列和报告“总计”列的 sql。
工作的 sql 代码如下,但是当我插入注释掉的“Case”代码时,我得到的错误似乎随着我纠正前面的错误而增加。
谁能告诉我应该在哪个位置插入“案例”以及代码有什么问题?

SELECT qItemSaleLines.ItemID, ;
qItems.ItemID, ;
qItemSaleLines.SaleID, ;
qSales.SaleID, ;
qSales.CardRecordID, ;
qCustomers.CardRecordID, ;
qItems.ItemNumber AS ProdCODE, ;
qItems.ItemName AS StkNAME, ;
qCustomers.LastName AS CUSTOMER, ;
qSales.InvoiceNumber AS SaleINVNo,  ;
qSales.InvoiceDate AS SaleDATE, ;
qItemSaleLines.Quantity AS SaleQTY, ;
qItemSaleLines.TaxExclusiveTotal AS SALE, ;
qItemSaleLines.CostOfGoodsSoldAmount AS COGS, ; 
qItemSaleLines.TaxExclusiveTotal - qItemSaleLines.CostOfGoodsSoldAmount AS MARGIN, ;
(qItemSaleLines.TaxExclusiveTotal - qItemSaleLines.CostOfGoodsSoldAmount) *  /        qItemSaleLines.TaxExclusiveTotal AS MPERCENT ;
FROM qItemSaleLines, qItems, qSales, qCustomers ; 
WHERE qSales.CardRecordID = qCustomers.CardRecordID AND qItemSaleLines.SaleID =    qSales.SaleID AND ;
qItemSaleLines.ItemID = qItems.ItemID AND qSales.InvoiceDate > {^2009-06-30} ;
ORDER BY qItems.ItemNumber, qSales.InvoiceDate ;


*!* (SELECT qItems.ItemID, qItemSaleLines.ItemID, qItemSaleLines.TaxExclusiveTotal, ; 
*!*         CASE WHEN qItems.ItemID = (SELECT TOP 1 qItems.ItemID FROM   qItems.ItemID, ;
*!*             WHERE qItems.ItemID = qItemSaleLines.ItemID, ;
*!*             ORDER BY qItems.ItemID desc), ;
*!*         THEN (SELECT SUM(qItemSaleLines.TaxExclusiveTotal) FROM qItemSaleLines.TaxExclusiveTotal,; 
*!*             WHERE qItems.ItemID <= qItemSaleLines.ItemID AND qItems.ItemID = qItemSaleLines.ItemID, ; 
*!*         ELSE ' ' END AS 'PROD-SALE'), ;
*!*     CASE WHEN qItems.ItemID = (SELECT TOP 1 qItems.ItemID FROM qItems.ItemID, ; 
*!*             ORDER BY qItems.ItemID desc), ;
*!*     THEN (SELECT SUM(qItemSaleLines.TaxExclusiveTotal) FROM qItemSaleLines.TaxExclusiveTotal, ;
*!*         ELSE ' ' END AS 'Grand Total') ;
4

2 回答 2

1

首先,VFP 不支持字段级别的 case-when 构造。此外,您在字段级别的子选择在 from 表之后和 where 之前,在 where 子句之后,在 order 之前有逗号......例如

select * from MyTable, where SomeCondition, Order by ...

如果进行 VFP 报告,您不必手动将组添加到原始数据中,这应该通过数据分组在报告本身中完成,并将您的 ItemID 添加为组的基础。然后将您的“可汇总”列复制/粘贴到每个项目的报告的组页脚带中。双击该字段,并为计算,告诉它 sum(),并在每个组的末尾重置(即:ItemID)。然后,包括一个报告摘要带。这会为整个报告打印一次......就像项目级别的摘要项目一样,再次复制/粘贴,但放入报告摘要带。双击这些元素到 sum() 并在 END OF REPORT 处重置。

但是,由于您在 VFP9 中运行,并且您可能希望使用已包含在其各自位置的行来转储数据,因此我将分解为单独的查询并将结果合并在一起,如下面的示例所示。在没有任何必要聚合的情况下预提取报表数据的行项目支持。

SELECT ;
        qItemSaleLines.ItemID, ; 
        qItemSaleLines.SaleID, ; 
        qCustomers.CardRecordID, ; 
        qItems.ItemNumber AS ProdCODE, ; 
        qItems.ItemName AS StkNAME, ; 
        qCustomers.LastName AS CUSTOMER, ; 
        qSales.InvoiceNumber AS SaleINVNo,  ; 
        qSales.InvoiceDate AS SaleDATE, ; 
        qItemSaleLines.Quantity AS SaleQTY, ; 
        qItemSaleLines.TaxExclusiveTotal AS SALE, ; 
        qItemSaleLines.CostOfGoodsSoldAmount AS COGS, ;  
        qItemSaleLines.TaxExclusiveTotal - qItemSaleLines.CostOfGoodsSoldAmount AS MARGIN, ; 
        (qItemSaleLines.TaxExclusiveTotal - qItemSaleLines.CostOfGoodsSoldAmount) / qItemSaleLines.TaxExclusiveTotal AS MPERCENT, ;
        "1" as TierLevel,;
        "1" as SubTier,;
        SPACE(50) as GroupCaption; 
    FROM ;
        qSales, ;
        qCustomers, ;  
        qItemSaleLines, ;
        qItems, ;
    WHERE ;
            qSales.CardRecordID = qCustomers.CardRecordID ;
        AND qSales.SaleID = qItemSaleLines.SaleID ;
        AND qItemSaleLines.ItemID = qItems.ItemID ;
        AND qSales.InvoiceDate > {^2009-06-30} ; 
    ORDER BY ;
        qItems.ItemNumber, ;
        qSales.InvoiceDate ; 
    INTO ;
        CURSOR C_TmpAllLineItemResults READWRITE 

*/ NOW, get your individual "ITEM GROUP" totals from ABOVE results...
SELECT ;
        TR.ItemID, ; 
        TR.ItemNumber AS ProdCODE, ; 
        TR.ItemName AS StkNAME, ; 
        "1" as TierLevel,;
        "2" as SubTier,;
        "Subtotal by " + TR.ItemNumber as GroupCaption; 
        SUM( TR.SaleQty ) as SaleQty,;
        SUM( TR.Sale ) as Sale,;
    FROM ;
        C_TmpAllLineItemResults TR;
    GROUP BY ;
        1, 2, 3, 4, 5, 6;
    ORDER BY ;
        1;
    INTO ;
        CURSOR C_SubTotalPerItem READWRITE 


*/ NOW, get your REPORT totals from ABOVE results...
SELECT ;
        "2" as TierLevel,;
        "Report Totals " as GroupCaption; 
        SUM( STBI.SaleQty ) as SaleQty,;
        SUM( STBI.Sale ) as Sale,;
    FROM ;
        C_SubTotalPerItem STBI;
    GROUP BY ;
        1, 2;
    INTO ;
        CURSOR C_ReportTotals READWRITE 

*/ Finally, merge them together... since the append from will add records with matching columns,
*/ even though a subtotal cursor doesn't have all the same columns, VFP doesn't care, just 
*/ adds for columns that DO match the table its going into.
SELECT C_TmpAllLineItemResults
APPEND FROM DBF( "C_SubTotalPerItem" )
APPEND FROM DBF( "C_ReportTotals" )

*/ Now, they are all merged... Finally, build an index for your report
INDEX on TierLevel + ItemID + SubTier TAG RptOrder

*/ Now, browse / review the order and adjust as needed...
于 2010-05-14T18:41:28.317 回答
1

此外,如果您只想将总计作为表格中的列,您可以执行类似的操作

select ;
       Tbl.YourColumns,;
       PerItem.TotalPerItem,;
       RptTotal.TotalPerAll;
   from ;
       YourOtherTables Tbl,;
       ( select YourSalesTable.ItemID,;
                sum( CalculatedSales ) as TotalPerItem;
             From;
                YourSalesTable;
             Group by ;
                ItemID ) PerItem,;
       ( select sum( CalculatedSales ) as TotalPerAll;
             From ;
                YourTalesTable ) RptTotal;
   where ;
       YourOtherJoinConditions;
      AND YourOtherTables.ItemID = PerItem.ItemID;
   order by ;
       whatever;
   into ;
       cursor YourReportResults

通过执行 SQL-Select 作为最后 2 个表(一个按 itemID 分组)将创建每个项目的总数。通过将最终 where 仅连接到别名 PerItem 的 ItemID,您将获得该列的总数。但是,由于别名 RptTotal 上没有连接,因此您将获得笛卡尔连接......但由于它始终是 1 条记录,因此每一行将具有与其“TotalPerAll”列相同的值。

我希望这两种解决方案可以满足您的需求。

于 2010-05-14T18:50:03.777 回答