根据日期范围计算数量之和

时间:2015-04-25 04:10:34

标签: sql sql-server

在下面的查询中,我有3个表GoodsReceivedNoteGoodsReceivedNoteDetailTransferNote。在GoodsReceivedNoteGoodsReceivedNoteDetail我在特定地点的特定日期收到产品。在TransferNote我将产品从一个位置转移到另一个位置。我的目标是将基于grn收到日期的startdate日期的产品转移数量grnend date中该产品的下一个接收日期[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

我的预期结果:

public static bool NumberOfEvensEqualsNumberOfOdds(IEnumerable<int> numbers)
{
    int evenCount = 0;
    int oddCount = 0;

    foreach (var item in numbers)
    {
        if (item % 2 == 0)
            evenCount++;
        else if (item % 2 != 0)
            oddCount++;  
    }

    return evenCount == oddCount;
}

2 个答案:

答案 0 :(得分:0)

这只是猜测。这部分内容对我没有意义。

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

答案 1 :(得分:0)

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

function comp(a, b) {

    return new Date(a.event_date) - new Date(b.event_date);
}

data=data.sort(comp)    

EndDate计算为grn中产品的下一个日期。 数量是在(包括)StartDate和grn中的EndDate之间的TransferNote中的TransferQuantity的总和。