2016-02-22 59 views
8

Używam polecenia kształtu ado w moim raporcie danych, działa dobrze, ale gdy moja funkcja zagregowana CALC (agrProfit/agrExtended * 100) ma wartość zerową lub 0/0 * 100 pokazuje ogólny błąd, a raport danych nie pojawia się. Proszę pomóż.VB6 CALC Agregacja na ADO Kształty po zwrocie Null uzyskiwanie Błąd ogólny w raporcie danych

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 
+0

Czy to w MS Access? –

+0

nope SQL Server – FatalError

Odpowiedz

2

Wygląda na to, że używasz tutaj ADO Data Shaping functions, a funkcja CALC(expression) umożliwia korzystanie z funkcji VBA wymienionych here w wyrażeniu. @ Sugestia C-Pound Guru powoduje błąd, ponieważ NULLIF() nie jest funkcją VBA, ale całe wyrażenie może być zapisane tak:

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

Daj mi znać, jeśli to dba o problemie.

+0

To działało na twojego Geniusza !!! – FatalError

1

Jeśli SQL Server 2005 lub nowszy można użyć NULLIF w połączeniu z ISNULL:

Wymień agrProfit/agrExtended z

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

ten powróci do zera, gdy agrExtended = 0 zamiast powodując podzielić przez zero błędu.

+0

CALC (ISNULL (agrProfit/NULLIF (agrExtended * 100,0), 0)) mówi NULLIF Kolumna została użyta w wyrażeniu CALC ale nie jest zdefiniowana w zestawie wierszy – FatalError

+0

Calc nie jest wbudowany Funkcja SQL (o ile wiem). Być może możesz pokazać tę funkcję i możemy zobaczyć, czy podział na zero może być obsłużony w tym miejscu ... –

0

Wygląda na to, że używasz MS Access lub czegoś, co łączy się z MS Access. Jeśli tak jest, być może można użyć Switch:

Wymienić:

CALC(agrProfit/agrExtended * 100)

Z:

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

Chodzi o to, aby zastąpić NULL 0, zastąpić dzielić przez 0 z 0, lub zwrócić rzeczywisty stosunek.

Powiązane problemy