用友U8物料最后一次出入库日期及计算实际库龄

这些SQL语句创建了多个视图,用于获取物料的库存相关信息,包括期初入库日期、最后一次采购入库日期、最后一次委外入库日期、最后一次其他入库日期、最后一次出库日期以及最后一次产成品入库日期,并计算了相应的库龄。视图通过ROW_NUMBER()函数对每个物料和仓库的入库记录进行排序,选取最新的日期,并用DATEDIFF()计算当前日期与入库日期之间的天数。
摘要由CSDN通过智能技术生成

--物料采购期初入库日期
ALTER VIEW V_Quick_PoQc
WITH ENCRYPTION
AS
SELECT Temp.cInvCode,
       Temp.cWhCode,
       Temp.dDate AS PoQcDate,
       DATEDIFF(dd, Temp.dDate, GETDATE()) AS 期初入库库龄
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords01.cInvCode,
                                           dbo.RdRecord01.cWhCode
                              ORDER BY RdRecord01.dDate DESC
                             ) AS id,
           rdrecords01.cInvCode,
           dbo.RdRecord01.cWhCode,
           CONVERT(VARCHAR(100), RdRecord01.dDate, 23) AS dDate
    FROM dbo.RdRecord01
        JOIN dbo.rdrecords01
            ON rdrecords01.ID = RdRecord01.ID
               AND RdRecord01.cSource IN ( '采购' )
) Temp
WHERE Temp.id = 1;
GO
--物料最有后一次采购入库日期
ALTER VIEW V_Quick_PoRd
WITH ENCRYPTION
AS
SELECT Temp.cInvCode,
       Temp.cWhCode,
       Temp.dDate AS PoDate,
       DATEDIFF(dd, Temp.dDate, GETDATE()) AS 采购入库库龄
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords01.cInvCode,
                                           dbo.RdRecord01.cWhCode
                              ORDER BY RdRecord01.dDate DESC
                             ) AS id,
           rdrecords01.cInvCode,
           dbo.RdRecord01.cWhCode,
           CONVERT(VARCHAR(100), RdRecord01.dDate, 23) AS dDate
    FROM dbo.RdRecord01
        JOIN dbo.rdrecords01
            ON rdrecords01.ID = RdRecord01.ID
               AND RdRecord01.cSource IN ( '采购订单', '采购到货单' )
) Temp
WHERE Temp.id = 1;

GO

--物料最后一次委外入库日期
ALTER VIEW V_Quick_OmRd
WITH ENCRYPTION
AS
SELECT Temp.cInvCode,
       Temp.cWhCode,
       Temp.dDate AS OmDate,
       DATEDIFF(dd, Temp.dDate, GETDATE()) AS 委外入库库龄
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords01.cInvCode,
                                           dbo.RdRecord01.cWhCode
                              ORDER BY RdRecord01.dDate DESC
                             ) AS id,
           rdrecords01.cInvCode,
           dbo.RdRecord01.cWhCode,
           CONVERT(VARCHAR(100), RdRecord01.dDate, 23) AS dDate
    FROM dbo.RdRecord01
        JOIN dbo.rdrecords01
            ON rdrecords01.ID = RdRecord01.ID
               AND RdRecord01.cSource IN ( '委外订单', '委外到货单' )
) Temp
WHERE Temp.id = 1;

GO
--物料最后一次其他入库日期
ALTER VIEW V_Quick_Rd
WITH ENCRYPTION
AS
SELECT Temp.cInvCode,
       Temp.cWhCode,
       Temp.dDate AS RdDate,
       DATEDIFF(dd, Temp.dDate, GETDATE()) AS 其他入库库龄
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords08.cInvCode,
                                           dbo.RdRecord08.cWhCode
                              ORDER BY RdRecord08.dDate DESC
                             ) AS id,
           rdrecords08.cInvCode,
           dbo.RdRecord08.cWhCode,
           CONVERT(VARCHAR(100), RdRecord08.dDate, 23) AS dDate
    FROM dbo.RdRecord08
        JOIN dbo.rdrecords08
            ON rdrecords08.ID = RdRecord08.ID
) Temp
WHERE Temp.id = 1;
GO
--物料最后一次出库日期
ALTER VIEW V_Quick_LastDate
WITH ENCRYPTION
AS
SELECT Tem.cInvCode,
       Tem.cWhCode,
       Tem.dDate AS LastDate,
       DATEDIFF(dd, Tem.dDate, GETDATE()) AS 最后出库天数
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY TEMP.cInvCode, TEMP.cWhCode ORDER BY TEMP.dDate DESC) AS id,
           TEMP.cInvCode,
           TEMP.cWhCode,
           TEMP.dDate
    FROM
    (
        SELECT *
        FROM
        (
            SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords01.cInvCode,
                                                   dbo.RdRecord01.cWhCode
                                      ORDER BY RdRecord01.dDate DESC
                                     ) AS id,
                   rdrecords01.cInvCode,
                   dbo.RdRecord01.cWhCode,
                   CONVERT(VARCHAR(100), RdRecord01.dDate, 23) AS dDate
            FROM dbo.RdRecord01
                JOIN dbo.rdrecords01
                    ON rdrecords01.ID = RdRecord01.ID
                       AND RdRecord01.cSource = '库存' --采购入库单中退货部分
            UNION ALL
            SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords11.cInvCode,
                                                   dbo.rdrecord11.cWhCode
                                      ORDER BY rdrecord11.dDate DESC
                                     ) AS id,
                   rdrecords11.cInvCode,
                   rdrecord11.cWhCode,
                   CONVERT(VARCHAR(100), rdrecord11.dDate, 23) AS dDate
            FROM dbo.rdrecord11
                JOIN dbo.rdrecords11
                    ON rdrecords11.ID = rdrecord11.ID
            UNION ALL
            SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords32.cInvCode,
                                                   dbo.rdrecord32.cWhCode
                                      ORDER BY rdrecord32.dDate DESC
                                     ) AS id,
                   rdrecords32.cInvCode,
                   rdrecord32.cWhCode,
                   CONVERT(VARCHAR(100), rdrecord32.dDate, 23) AS dDate
            FROM rdrecord32
                JOIN rdrecords32
                    ON rdrecords32.ID = rdrecord32.ID
            UNION ALL
            SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords09.cInvCode,
                                                   dbo.RdRecord09.cWhCode
                                      ORDER BY RdRecord09.dDate DESC
                                     ) AS id,
                   rdrecords09.cInvCode,
                   RdRecord09.cWhCode,
                   CONVERT(VARCHAR(100), RdRecord09.dDate, 23) AS dDate
            FROM RdRecord09
                JOIN rdrecords09
                    ON rdrecords09.ID = RdRecord09.ID
        ) A
        WHERE A.id = 1
    ) TEMP
) Tem
WHERE Tem.id = 1;

GO
--物料最后一次产成品入库日期
ALTER VIEW V_Quick_CpRd
WITH ENCRYPTION
AS
SELECT Temp.cInvCode,
       Temp.cWhCode,
       Temp.dDate AS CpRdDate,
       DATEDIFF(dd, Temp.dDate, GETDATE()) AS 产成品入库库龄
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY rdrecords10.cInvCode,
                                           rdrecord10.cWhCode
                              ORDER BY rdrecord10.dDate DESC
                             ) AS id,
           rdrecords10.cInvCode,
           rdrecord10.cWhCode,
           CONVERT(VARCHAR(100), rdrecord10.dDate, 23) AS dDate
    FROM rdrecord10
        JOIN rdrecords10
            ON rdrecords10.ID = rdrecord10.ID
) Temp
WHERE Temp.id = 1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值