Sql 根据日期范围计算数量总和的步骤

Sql 根据日期范围计算数量总和的步骤,sql,sql-server,Sql,Sql Server,在下面的查询中,我有3个表GoodsReceivedNote、GoodsReceivedNoteDetail和TransferNote。在GoodsReceivedNote和GoodsReceivedNote详细信息中,我在特定日期、特定地点收到产品。在TransferNote中,我将产品从一个位置转移到另一个位置。我的目标是根据收货日期的grn日期作为起始日期和该产品在grn中的下一个收货日期作为结束日期,对产品的转移数量求和。这里我将展示这个例子 [GoodsReceivedNote]

在下面的查询中,我有3个表GoodsReceivedNote、GoodsReceivedNoteDetail和TransferNote。在GoodsReceivedNote和GoodsReceivedNote详细信息中,我在特定日期、特定地点收到产品。在TransferNote中,我将产品从一个位置转移到另一个位置。我的目标是根据收货日期的grn日期作为起始日期和该产品在grn中的下一个收货日期作为结束日期,对产品的转移数量求和。这里我将展示这个例子

[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 
dbo.fn_MaterialTransferqty(GRN.LocationID,GRND.ProductID,GRND.CreatedON,GRND.CreatedON )
From GoodsReceivedNoteDetail GRND 
LEFT OUTER JOIN GoodsReceivedNote GRN ON GRN.GoodsReceivedNoteID =GRND.GoodsReceivedNoteID
LEFT OUTER JOIN TransferNote TN ON Tn.ProductID=GRND.ProductID

Select GRN.LocationID,GRND.ProductID,GRND.ReceivedQuantity,
dbo.fn_MaterialTransferqty(GRN.LocationID,GRND.ProductID,GRND.CreatedOn,GRND.CreatedOn)
From GoodsReceivedNoteDetail GRND 
LEFT OUTER JOIN GoodsReceivedNote GRN ON GRN.GoodsReceivedNoteID =GRND.GoodsReceivedNoteID
LEFT OUTER JOIN TransferNote TN ON Tn.ProductID=GRND.ProductID
WHERE GRND.CreatedOn >=@i_StartDate AND   GRND.CreatedOn<=@i_EndDate

这只是一个猜测。这其中的一部分对我来说没有意义

select
    grn.GoodsReceivedNoteID, grn.LocationID,
    grnd.AcceptedQuantity, grnd.ProductID, grnd.CreatedOn,
    tn.Fromlocation, tn.Tolocation, tn.ProductID, /* why do you want this twice? */
    sum(TransferQuantity) as TransferQuantity
from
    GoodsReceivedNote as grn
    inner join GoodsReceivedNoteDetail as grnd
        on  grnd.GoodsReceivedNoteID = grn.GoodsReceivedNoteID
    inner join TransferNote as tn
        on  tn.Fromlocation = grn.LocationID
            and tn.ProductID = grn.ProductID /* is this correct? */
group by
    grn.GoodsReceivedNoteID, grn.LocationID,
    grnd.AcceptedQuantity, grnd.ProductID, grnd.CreatedOn,
    tn.Fromlocation, tn.Tolocation, tn.ProductID

这将根据产品grn开始和结束日期返回,并将这两个日期之间的转移数量相加:

select distinct grn.ProductID, grn.CreatedON StartDate, 
    (select min(CreatedON) from GoodsReceivedNoteDetail 
        where ProductID = grn.ProductID and CreatedON > grn.CreatedON
    ) EndDate,
    (select sum(TransferQuantity) from TransferNote
        where ProductID = grn.ProductID
        and CreatedOn >= grn.CreatedON 
        and CreatedOn <= 
            (select min(CreatedON) from GoodsReceivedNoteDetail 
                where ProductID = grn.ProductID and CreatedON > grn.CreatedON)
    ) Quantity
from GoodsReceivedNoteDetail grn
EndDate计算为grn中产品的下一个日期。
数量是TransferNote中的TransferQuantity的总和,包括grn中的开始日期和结束日期。

简单的分组方式和总和似乎可以实现这一目的。但是我不太确定你在这个函数里面有什么。@shawnt00你能帮我查询一下吗