2016-02-22 22 views
8

Sto usando il comando ado shape sul mio report dei dati, funziona bene ma quando la mia funzione aggregata CALC (agrProfit/agrExtended * 100) è nullo o 0/0 * 100 mostra un errore generale e il report dei dati non viene mostrato. Per favore aiuto.VB6 CALC Aggregazione su ADO Shape quando restituisce Null ottenendo errore generale nel report dei dati

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

È presente in MS Access? –

+0

no SQL Server – FatalError

risposta

2

Quindi sembra che tu stia utilizzando lo ADO Data Shaping functions qui e lo CALC(expression) consente di utilizzare le funzioni VBA elencate here all'interno dell'espressione. @ Suggerimento di C-Pound Guru causa un errore in quanto NULLIF() non è una funzione VBA, ma l'intera espressione può essere riscritta così:

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

fatemi sapere se questo si prende cura del tuo problema.

+0

Ha funzionato il tuo Genius !!! – FatalError

1

Se SQL Server è il 2005 o più recente è possibile utilizzare NULLIF in combinazione con ISNULL:

Sostituire agrProfit/agrExtended con

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

Questo restituirà zero quando agrExtended = 0 piuttosto che causare un dividi per errore zero.

+0

CALC (ISNULL (agrProfit/NULLIF (agrExtended * 100,0), 0)) si dice NULLIF Colonna è stato utilizzato in un'espressione CALC ma non è definito nel set di righe – FatalError

+0

Calc non è una costruito nel Funzione SQL (per quanto ne so). Forse puoi mostrare quella funzione e possiamo vedere se la divisione per zero può essere gestita lì ... –

0

Sembra che si sta utilizzando MS Access o qualcosa che si interfaccia con MS Access. Se questo è il caso, forse è possibile utilizzare Switch:

Sostituire:

CALC(agrProfit/agrExtended * 100)

Con:

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

L'idea è quella di sostituire NULL con 0, sostituire divisione per 0 con 0, oppure restituire il rapporto attuale.

Problemi correlati