在SQL Query-MS Access中使用用户定义的函数

时间:2017-04-17 19:50:36

标签: sql ms-access

我使用以下查询:

SELECT DISTINCT Purchases.PONumber, SalesOrders.SalesOrderNumber, Bookings.VessalVoyage, Purchases.Quantity, Bookings.ShippingLine, Bookings.CargoCutoffDate, SalesOrders.Quantity AS Quant, SalesOrders.QuantityUOM AS QuantUOM, Bookings.BookingNumber, Shipments.FreightRate, Purchases.VendorID, Shipments.ShipmentInvoiceNumber, Shipments.InvoicePIDate, Bookings.SailingDate, Bookings.PortOfLoading, Bookings.ArrivalDate, Containers.InspectionCharge, SalesOrders.Grade, Purchases.NumberOfContainers, TruckingRates.TruckingRate, [Purchases.NumberOfContainers]*[TruckingRates.TruckingRate] AS TruckCost, IIf(IsNull([Purchases.NumberOfContainers]*[Containers.InspectionCharge]),0,[Purchases.NumberOfContainers]*[Containers.InspectionCharge]) AS Inspt, SalesOrders.SIHUID, SalesOrders.SIHUShippingAssistant, (IIf([BuyPriceUOM]='sht',[BuyPrice],IIf([BuyPriceUOM]='kg',[BuyPrice]*907.185,IIf([BuyPriceUOM]='MT',[BuyPrice]*0.907185,IIf([BuyPriceUOM]='lbs',[BuyPrice]*2000.000574,[BuyPrice]))))/IIf(Nz([BuyPriceCurrency],"USD")="USD",1,Nz([BuyPriceExchangeRate],0))) AS Buy,Query6.Load
FROM (((((Purchases INNER JOIN Containers 
ON Purchases.PONumber = Containers.PONumber) 
INNER JOIN SalesOrders 
ON Containers.SalesOrderNumber = SalesOrders.SalesOrderNumber) 
INNER JOIN Bookings 
ON Containers.BookingID = Bookings.BookingID) 
INNER JOIN Shipments 
ON Containers.ShipmentID = Shipments.ShipmentID) 
LEFT JOIN TruckingRates 
ON Containers.TruckingRateID = TruckingRates.TruckingRateID) 
LEFT JOIN Query6 
ON Containers.PONumber = Query6.PONumber
GROUP BY Purchases.PONumber, SalesOrders.SalesOrderNumber, Bookings.VessalVoyage, Purchases.Quantity, Bookings.ShippingLine, Bookings.CargoCutoffDate, SalesOrders.Quantity, SalesOrders.QuantityUOM, Bookings.BookingNumber, Shipments.FreightRate, Purchases.VendorID, Shipments.ShipmentInvoiceNumber, Shipments.InvoicePIDate, Bookings.SailingDate, Bookings.PortOfLoading, Bookings.ArrivalDate, Containers.InspectionCharge, SalesOrders.Grade, Purchases.NumberOfContainers, TruckingRates.TruckingRate, SalesOrders.SIHUID, SalesOrders.SIHUShippingAssistant, Containers.NetWeight, Purchases.BuyPrice, Purchases.BuyPriceCurrency, Purchases.BuyPriceUOM, Purchases.BuyPriceExchangeRate, Containers.WeightUOM, SalesOrders.UnitPrice, Containers.SalesOrderNumber, Containers.ContainerNo,Query6.Load;

在Query6中,我使用函数来获取“WtInMT”: 问题6:

SELECT CInt(Round(GetWeightInMT(Sum([NetWeight]),WeightUOM),0)) AS Load, Containers.PONumber
FROM Containers
GROUP BY Containers.SalesOrderNumber, Containers.WeightUOM, Containers.PONumber;

功能:

Public Function GetWeightInMT(WtToConvert As Double, WtInUOM As String) As Double
    ' MT(Metric Tons)
    ' Pounds(lbs)      to MT: lbs / 2204.6 = MT
    ' Kilograms(kg)    to MT: kg * .001 = MT
    ' Short tons(sh t) to MT: sh t * .907185 = MT.

    Select Case WtInUOM
        Case "lbs"
            GetWeightInMT = WtToConvert / 2204.6
        Case "kg"
            GetWeightInMT = WtToConvert * 0.001
        Case "sh t"
            GetWeightInMT = WtToConvert * 0.907185
        Case Else
            GetWeightInMT = WtToConvert
    End Select
    If GetWeightInMT < 1 Then GetWeightInMT = 0

End Function

当我运行查询时,我得到数据不匹配错误。

任何建议? 三江源

0 个答案:

没有答案