在我的查询中,我有3个表GoodsReceivedNote
,GoodsReceivedNoteDetail
和TransferNote
。在GoodsReceivedNoteDetails
我收到的产品和转让说明我正在转移产品。
例如:我在10-2-2015 50公斤时收到产品名称盐,我将产品转移到10-2-2015 10kg和12-2-2015 25 kg的另一个地方。我收到产品名称盐1-3-15 100公斤。并将产品转移到5-3-2015 50公斤的另一个地方。现在我想在(10-2-2015 -31-2-2015)之间总计特定grn id(即)的总转移量。
GoodsReceivedNote
:
GoodsReceivedNoteID | LocationID
1 | 1
2 | 1
GoodsReceivedNoteDetail
:
GoodsReceivedNoteDetailID|GoodsReceivedNoteID|AcceptedQuantity|ProductID|CreatedON|
1 | 1 | 50 | 1 |10-2-2015
2 | 2 | 100| 1 |1-3-2015
TransferNote
:
Fromlocation |Tolocation|ProductID|TransferQuantity|CreatedOn|
1 2 1 10|10-2-2015
1 2 1 25 |12-2-2015
1 2 1 50 |5-3-2015``
我的预期结果:
GoodsReceivedNoteID
|LocationID|AcceptedQuantity|ProductID|CreatedOn|Fromlocation |Tolocation|ProductID|TransferQuantity
1 | 1 | 50 | 1 | 10-2-2015 | 1 | 2 | 1 | 35 |
2 | 2 | 100 | 1 | 1-3-15 | 1 | 2 | 1 | 50 |
查询:
SELECT DISTINCT
[GRN].[GoodsReceivedNoteNo] ,
[GRN].[LocationID] ,
[GRN].[CreatedOn] ,
[POD].[UnitPrice] AS [BasicRate] ,
[POD].[VAT] ,
[POD].[UnitPrice] * [GRND].[ReceivedQuantity] AS [CostofPurchase] ,
[PO].[PurchaseOrderNo] ,
[V].[VendorName] ,
[GRN].[SupplierInvoiceNo] ,
[GRN].[SupplierInvoiceDate] ,
[SR].[LRNO] ,
[T].[TransporterName] ,
SUBSTRING([LPM].[BarCode], 4, 4) [Code] ,
RIGHT([LPM].[BarCode], 6) [ProductCode] ,
[P].[ProductName] ,
[UT].[UnitTypeShortCode] ,
CAST([GRND].[ReceivedQuantity] AS DECIMAL(18, 2)) AS [ReceivedQuantity] ,
CAST([GRND].[RejectedQuantity] AS DECIMAL(18, 2)) AS [RejectedQuantity] ,
CAST([GRND].[AcceptedQuantity] AS DECIMAL(18, 2)) AS [AcceptedQuantity] ,
CAST([GRND].[UnitPrice] AS DECIMAL(18, 2)) AS [BasicRate] ,
CAST(ISNULL(( [GRND].[UnitPrice] * [GRND].[ReceivedQuantity] ), 0.00) AS DECIMAL(18, 2)) [BasicValue] ,
[GRND].[VAT] ,
CASE WHEN COUNT([GRND].[GoodsReceivedNoteID]) OVER ( PARTITION BY [GRND].[GoodsReceivedNoteID] ) > 1
THEN ISNULL(( [GRND].[FreightCharges] ), 0.00)
ELSE ISNULL(( [OEC].[FreightCharges] ), 0.00)
END [FreightApportioned] ,
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 [LoadingandUnloadingApportioned] ,
CASE WHEN COUNT([GRND].[GoodsReceivedNoteID]) OVER ( PARTITION BY [GRND].[GoodsReceivedNoteID] ) > 1
THEN ISNULL(( [GRND].[FreightCharges] ), 0.00) + ISNULL(( [GRND].[LoadingCost] + [GRND].[UnloadingCost] ), 0.00)
+ ISNULL(( [GRND].[UnitPrice] ), 0.00)
ELSE ISNULL(( [OEC].[FreightCharges] ), 0.00) + ISNULL(( [OEC].[LoadingCost] + [OEC].[UnloadingCost] ), 0.00)
+ ISNULL(( [GRND].[UnitPrice] ), 0.00)
END [TotalCost] ,
[TN].[CreatedOn] ,
[TN].[TransferQuantity] ,
[TN].[Tolocation] ,
[TN].[ProductID] ,
[GRND].[ProductID] ,
( [TN].[TransferQuantity] * [TN].[UnitPrice] ) AS [VALUE] ,
[L].[Location] ,
[TN].[FreightCharge] ,
( [TN].[LoadingCharge] + [TN].[UnLoadingCharge] ) AS [LoadingandUnloadingCharges] ,
( [TN].[FreightCharge] + [TN].[LoadingCharge] + [TN].[UnLoadingCharge] ) AS [LoadingCost]
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]
LEFT OUTER JOIN
[LocationProductMap] [LPM] ON [LPM].[ProductID] = [GRND].[ProductID]
LEFT OUTER JOIN
[UnitType] [UT] ON [UT].[UnitTypeID] = [P].[UnitTypeID]
LEFT OUTER JOIN
[PurchaseOrderDetail] [POD] ON [POD].[PurchaseOrderID] = [PO].[PurchaseOrderID]
LEFT OUTER JOIN
[TransferNote] [TN] ON [TN].[ProductID] = [GRND].[ProductID]
LEFT OUTER JOIN
[Locations] [L] ON [L].[LocationID] = [TN].[Tolocation]
WHERE
(@d_StartDate = ''
OR CAST([GRND].[CreatedOn] AS DATE) >= @d_StartDate
OR CAST([GRND].[CreatedOn] AS DATE) = @d_StartDate)
AND (@d_EndDate = ''
OR CAST([GRND].[CreatedOn] AS DATE) <= @d_EndDate)
AND (@i_GrnStartNo = '0'
OR [GRN].[GoodsReceivedNoteNo] >= @i_GrnStartNo)
AND (@i_GrnEndNo = '0'
OR [GRN].[GoodsReceivedNoteNo] <= @i_GrnEndNo)
AND ([GRN].[LocationID] = @i_LocationID)
--AND CAST( GRND.CreatedOn AS DATE)=@d_StartDate
答案 0 :(得分:0)
对此不完全清楚。
(@d_StartDate = ''
OR CAST([GRND].[CreatedOn] AS DATE) >= @d_StartDate
OR CAST([GRND].[CreatedOn] AS DATE) = @d_StartDate)
第三部分是副本 - 将被上面的条件捕获。
关于&#34;我想根据特定的grn&#34;来计算转移数量。请澄清。您已经在WHERE子句中有日期条件,并且顶部没有任何SUM的迹象,因此我不清楚您想要更改什么以及在哪里。
SUM([TN].[TransferQuantity]) OVER (PARTITION BY GRN.LocationID)
例如,在您的select语句中,会保留您的日期选择标准,但会为您提供该位置的小计。而像
这样的东西SUM(CASE WHEN CAST( GRND.CreatedOn AS DATE)=@d_StartDate THEN [TN].[TransferQuantity] ELSE 0 END) OVER (PARTITION BY GRN.LocationID)
只会在日期是提供的日期时为您提供该位置ID的小计。