【问题标题】:VB6 CALC Aggregation on ADO Shape when returns Null getting General Error in Data Report返回 Null 时 ADO 形状上的 VB6 CALC 聚合在数据报告中出现一般错误
【发布时间】:2016-02-22 09:20:38
【问题描述】:

我在我的数据报告中使用 ado shape 命令,它工作正常,但是当我的聚合函数 CALC(agrProfit/agrExtended*100) 为空或 0/0*100 时,它显示一般错误并且数据报告未显示。请帮忙。

mRS.Open "SHAPE {select products.productid,products.productcode,isnull(products.description,descr) as description,isnull(vendor.description,'*** NOT FOUND ***') as groupdescription, " & _
    "isnull(sum(totalcost),0) as mTotalCost,isnull(sum(extended) - (sum(totalcost)),0) as mProfit,  " & _
    "sum(charges) as mCharges,sum(discount) as mDiscounts, sum(retextended) as mReturns, " & _
    "reportuom, sum(totalcost) as mTotalCost, isnull(case when sum(extended) = 0 then 0 else (sum(extended) - (sum(totalcost)))/sum(extended)*100 end,0)  as mgpm, sum(totalcost) as mTotalCost, case when sum(extended) = 0 then 0 else (sum(extended) - (sum(totalcost)))/sum(extended)*100 end  as mgpm, sum(case when extended < 0 then  (0 - (totalqty/products.reportqty))  else (totalqty/products.reportqty) end) as mTotalQty, isnull(sum(extended),0) as mExtended,  sum(case when extended < 0 then  (0 - (totalqty/products.reportqty)) else (totalqty/products.reportqty) end) / " & mTotalQty & " * 100 as mPercTotalQty, sum(extended) / " & mTotalExtended & " * 100 as mPercExtended " & _
    "From " & _
        "(select finishedsales.QtyReturned,finishedsales.productid,finishedsales.description as descr, finishedsales.averageunitcost* case when [return]=1 then convert(money,0-totalqty) else totalqty end as TotalCost,(chargeallowance * qty) + (chargeamountdiscounted * qty) as charges,(allowance * qty) + (amountdiscounted * qty)+ (extended-(extended * multiplier)) as discount,0 as rettotalqty, 0 as retextended,totalqty,round(extended * multiplier,4) as extended  From finishedsales " & _
        " left join products on products.productid = finishedsales.productid " & _
        .gReportCriteria & _
        "Union All " & _
        "select finishedsales.QtyReturned, finishedsales.productid,finishedsales.description as descr,0 as totalcost,0 as charges,0 as discount,totalqty as rettotalqty ,abs(round(extended,4)) as retextended,0 as totalqty, 0 as extended From finishedsales " & _
            "left join products on products.productid = finishedsales.productid " & _
        Replace(UCase(.gReportCriteria & " and [RETURN] = 1"), "[RETURN] = 0", "[return] = 1") & _
    ") as finishedsales " & _
    "left join products on products.productid=finishedsales.productid  " & _
    "left join vendor on products.vendorcode=vendor.vendorcode " & _
    "group by descr,products.productid,products.productcode,products.description,vendor.description,reportuom " & _
    "order by groupdescription, " & IIf(frmReportProducts.chkTop And fVal(frmReportProducts.txtTop) > 0, "finishedsales.mtotalqty desc,", "") & " products.description}  AS Command1 COMPUTE Command1, SUM(Command1.mTotalQty) AS agrTotalQty,  SUM(Command1.mExtended) AS agrExtended, SUM(Command1.mProfit) AS agrProfit, CALC(agrProfit/agrExtended*100) As agrGPM BY groupdescription", mcn

【问题讨论】:

  • 这是在 MS Access 中吗?

标签: sql-server vb6 ado


【解决方案1】:

所以看起来您在这里使用了ADO Data Shaping functions,而CALC(expression) 允许您使用表达式中列出的here 的VBA 函数。 @C-Pound Guru 的建议会导致错误,因为 NULLIF() 不是 VBA 函数,但整个表达式可以这样重写:

CALC(IIF(IsNull(agrProfit), 0, IIF(agrProfit=0, 0, agrProfit/agrExtended) *100))

如果这能解决您的问题,请告诉我。

【讨论】:

    【解决方案2】:

    如果您的 SQL Server 是 2005 或更高版本,您可以将 NULLIFISNULL 结合使用:

    agrProfit/agrExtended 替换为

    ISNULL(agrProfit / NULLIF(agrExtended,0),0)
    

    这将在 agrExtended=0 时返回零,而不是导致除以零错误。

    【讨论】:

    • CALC(ISNULL(agrProfit/NULLIF(agrExtended*100,0),0)) 它说 NULLIF 列已在 CALC 表达式中使用,但未在行集中定义
    • Calc 不是内置的 SQL 函数(据我所知)。也许你可以展示这个函数,我们可以看看那里是否可以处理除以零...
    【解决方案3】:

    您似乎正在使用 MS Access 或与 MS Access 接口的东西。如果是这样的话,也许你可以使用Switch

    替换:

    CALC(agrProfit / agrExtended * 100)

    与:

    Switch(
      ISNULL(SUM(Command1.mExtended)), 0,
      ISNULL(SUM(Command1.mProfit)), 0,
      IIF(SUM(Command1.mExtended) = 0, 0, SUM(Command1.mProfit) / SUM(Command1.mExtended) * 100)
          )
    

    思路是将NULL替换为0,将除以0替换为0,否则返回实际比例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-08
      • 2015-10-05
      • 2022-11-18
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      相关资源
      最近更新 更多