基于Date out / In链接两行数据

时间:2012-11-25 03:47:01

标签: sql-server fifo

架构和示例数据

下面是一些显示表格布局的示例数据。在此示例中,我仅显示特定ITEM和SERIAL的事务。然而,在生产中,将会有其他ITEMS / SERIALS,因此所执行的任何连接都需要在这两个领域。

transid Item                        qty Transdate               Type    Serial  IssuedTo
======= =========================== === ======================= ======= ======= ========
423371  SUNNENBOREGAGE1.18-2.0-3    1   2012-10-22 08:49:18.000 ISSRT   G-02145 742
422415  SUNNENBOREGAGE1.18-2.0-3    -1  2012-10-17 18:42:08.000 RETN    G-02145 139
422082  SUNNENBOREGAGE1.18-2.0-3    1   2012-10-17 13:21:38.000 REISS   G-02145 139
421518  SUNNENBOREGAGE1.18-2.0-3    1   2012-10-15 13:37:08.000 ISSRT   G-02145 709
421459  SUNNENBOREGAGE1.18-2.0-3    -1  2012-10-15 09:37:41.000 RETN    G-02145 640
421426  SUNNENBOREGAGE1.18-2.0-3    1   2012-10-15 08:02:19.000 ISSRT   G-02145 640
420100  SUNNENBOREGAGE1.18-2.0-3    -1  2012-10-10 06:23:34.000 RETN    G-02145 742
420097  SUNNENBOREGAGE1.18-2.0-3    1   2012-10-10 06:22:21.000 ISSRT   G-02145 742

所有退货均由RETN或REISS类型标记。所有问题都由一种ISSRT或REISS标记。重新发行(REISS)既是回归也是问题。 ISSUEDTO字段标记谁收到了REISS或ISSRT交易的项目。

期望的结果

简而言之,我想查找日期范围(季度)内的所有退货以及匹配的发货交易(无论何时发出),以便我可以识别发给它的员工和长度(以天为单位)该员工持有该特定项目/序列的时间。

我的最终结果集将包含以下列:

ITEM SERIAL EMPLOYEE ISSUETRANSID ISSUEDATE RETURNTRANSID RETURNDATE DAYSOUT

我似乎无法理解如何执行此操作,因为REISS在技术上是一行中的两个事务并确保每个ISSRT(或REISS)事务仅针对每个RETN(或REISS)事务使用一次。这也听起来很像FIFO / LIFO问题,但我似乎无法应用这里的逻辑(这可能只是我自己的缺点)。

任何帮助或指示都将非常感谢!谢谢=)

1 个答案:

答案 0 :(得分:0)

经过更多的研究,我相信我找到了问题的答案。使用以下帖子中的信息是我需要的。

SQL Server: How to Join to first row

我的代码(具体而且可能与我的示例数据没有明确匹配):

SELECT 
    ReturnedItems.SerialID AS SerialID,
    ReturnedItems.Item AS R_Item,
    ReturnedItems.TransNumber AS R_TransNumber,
    ReturnedItems.TransDate AS R_TransDate,
    ReturnedItems.TypeDescription AS R_TransDescription,
    ReturnedItems.IssuedTo AS R_IssuedTo,
    IssuedItems.TransNumber AS I_TransNumber,
    IssuedItems.TransDate AS I_TransDate,
    IssuedItems.TypeDescription AS I_TransDescription,
    IssuedItems.IssuedTo AS I_IssuedTo,
    DATEDIFF(HOUR, IssuedItems.TransDate, ReturnedItems.TransDate) / 24 AS Days_Out
FROM 
    CribMaster.dbo.TRANS AS ReturnedItems CROSS APPLY (
        SELECT TOP 1 
            CribMaster.dbo.TRANS.Item AS Item,
            CribMaster.dbo.TRANS.SerialID AS SerialID,
            CribMaster.dbo.TRANS.TransNumber AS TransNumber,
            CribMaster.dbo.TRANS.TransDate AS TransDate,
            CribMaster.dbo.TRANS.TypeDescription AS TypeDescription,
            CribMaster.dbo.TRANS.IssuedTo AS IssuedTo
        FROM 
            CribMaster.dbo.TRANS 
        WHERE
            CribMaster.dbo.TRANS.SerialID = ReturnedItems.SerialID AND 
            CribMaster.dbo.TRANS.TypeDescription IN ('ISSRT', 'REISS') AND 
            CribMaster.dbo.TRANS.TransDate < ReturnedItems.TransDate
        ORDER BY 
            CribMaster.dbo.TRANS.TransDate DESC
    ) AS IssuedItems
WHERE 
    ReturnedItems.TypeDescription IN ('RETN', 'REISS') AND 
    ReturnedItems.TransDate < '10/1/2012' AND
    ReturnedItems.TransDate >= '7/1/2012'