Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server查询的问题_Sql Server - Fatal编程技术网

Sql server SQL Server查询的问题

Sql server SQL Server查询的问题,sql-server,Sql Server,在下面的查询中,我从otherexpensecost表中选择了loadingcost和unloadingcost以及运费。但我想检查goodsreceivednoteid计数在GoodsReceivedNoteDetails表中是否大于1,它应该从GoodsReceivedNoteDetails表中获取,否则应该从otherexpensecost表中获取装卸费和运费。但是在下面的查询中,我从otherexpense成本表中获取装载成本、卸载成本、运费,而没有在GoodsReceivedNoteD

在下面的查询中,我从otherexpensecost表中选择了loadingcost和unloadingcost以及运费。但我想检查goodsreceivednoteid计数在GoodsReceivedNoteDetails表中是否大于1,它应该从GoodsReceivedNoteDetails表中获取,否则应该从otherexpensecost表中获取装卸费和运费。但是在下面的查询中,我从otherexpense成本表中获取装载成本、卸载成本、运费,而没有在GoodsReceivedNoteDetail中检查GoodsReceivedNoteID的编号

表1其他费用成本:

GoodsReceivedNoteID
LoadingCost
UnloadingCost
FreightCharges
表2收到的货物说明详情

GoodsReceivedNoteID
GoodsreceivedNoteDetailID
ProductID
LoadingCost
UnloadingCost
FreightCharges  
查询:

SELECT      
    GRN.GoodsReceivedNoteNo,
    GRN.LocationID,
    GRN.CreatedOn,
    PO.PurchaseOrderNo,
    V.VendorName,
    GRN.SupplierInvoiceNo,
    GRN.SupplierInvoiceDate,
    SR.LRNO,
    T.TransporterName,
    ISNULL((OEC.FreightCharges), 0.00) FreightCharge,
    ISNULL((OEC.LoadingCost+OEC.UnloadingCost), 0.00) LoadingandUnloadingcharges,
    P.ProductCode,
    P.ProductName,
    GRND.ReceivedQuantity,
    GRND.RejectedQuantity,
    GRND.AcceptedQuantity,
    GRND.UnitPrice AS BasicRate,
    ISNULL((GRND.UnitPrice * GRND.ReceivedQuantity), 0.00) BasicValue,
    GRND.VAT,
    ISNULL((OEC.FreightCharges), 0.00) AS FreightApporitioned,
    ISNULL((OEC.LoadingCost + OEC.UnloadingCost), 0.00) AS LoadingandUnloadingApportioned,
    ISNULL((GRND.UnitPrice + OEC.FreightCharges + OEC.LoadingCost + OEC.UnloadingCost), 0.00) AS TotalCost
FROM 
    GoodsReceivedNoteDetail GRND
LEFT OUTER JOIN 
    GoodsReceivedNote GRN ON GRN.GoodsReceivedNoteID = GRND.GoodsReceivedNoteID 
LEFT OUTER JOIN 
    PurchaseOrder PO ON PO.PurchaseOrderID = GRN.PurchaseOrderID
LEFT OUTER JOIN 
    Vendor V ON V.VendorID = PO.VendorID
LEFT OUTER JOIN 
    SecurityRegister SR ON SR.SecurityRegisterID = GRN.SecurityRegisterID
LEFT OUTER JOIN 
    Transporter T ON T.TransporterID = SR.TransporterID
LEFT OUTER JOIN 
    OtherExpenseCost OEC ON OEC.GoodsReceivedNoteID = GRN.GoodsReceivedNoteID 
LEFT OUTER JOIN 
    Product P ON P.ProductID = GRND.ProductID
WHERE 
   GRND.CreatedOn >= @d_StartDate 
   AND  GRN.LocationID = @i_LocationID 
   AND GRND.CreatedOn <= @d_EndDate 
END
使用窗口函数查找每个GoodsReceivedNoteID的计数。试试这样的

Select
......
CASE
    WHEN Count(GRND.GoodsReceivedNoteID) OVER(partition BY GRND.GoodsReceivedNoteID) > 1 
    THEN Isnull(( GRND.FreightCharges ), 0.00)
    ELSE Isnull(( OEC.FreightCharges ), 0.00)
END FreightCharge,
CASE
    WHEN Count(GRND.GoodsReceivedNoteID) OVER(partition BY GRND.GoodsReceivedNoteID) > 1 
    THEN Isnull(( GRND.LoadingCost + GRND.UnloadingCost ), 0.00)
    ELSE Isnull(( OEC.LoadingCost + OEC.UnloadingCost ), 0.00)
END LoadingandUnloadingcharges 
.....