在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') ;
答案 0 :(得分:1)
首先,VFP不支持字段级别的case-when构造。此外,您在字段级别的子选择在from表之后和where之前以及在where子句之后,在订单之前有逗号...例如
select * from MyTable, where SomeCondition, Order by ...
如果要执行VFP报告,则不必手动将组中的行添加到原始数据中,这应该通过数据分组在报告本身中完成,并添加ItemID作为组的基础。然后将“可汇总”列复制/粘贴到每个项目的报告的组页脚区域中。双击该字段,并进行计算,将其告诉sum(),并在每个组的末尾重置(即:ItemID)。然后,包括报告摘要带。这将为整个报告打印一次...与项目级别的摘要项目一样,再次复制/粘贴,但将其放入报告摘要区域。双击这些元素sum()并在END OF REPORT重置。
但是,由于您在VFP9中运行,并且您可能希望将数据与已经包含在各自位置的行进行DUMP,我将分解为单独的查询并将结果合并在一起,如下所示。在没有任何必要的聚合的情况下,预先提取报表数据的行项目支持。
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...
答案 1 :(得分: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分组)将创建每个项目的总数。通过将final连接到别名PerItem的ItemID,您将得到总数为的列。但是,由于NO连接在别名RptTotal上,您将获得笛卡尔连接...但由于它始终为1条记录,因此每行将具有与其“TotalPerAll”列相同的值。
我希望这两种解决方案能够满足您的需求。