用友U8出入库流水账查询SQL

SELECT a.ID,
       dDate,
       a.AutoID,
       (CASE
            WHEN a.bRdFlag <> 0 THEN
                N'入库'
            ELSE
                N'出库'
        END
       ) AS SFBZ,
       CONVERT(VARCHAR(10), a.dVDate, 102) AS SXRQ,
       CONVERT(VARCHAR(10), a.dmadedate, 121) AS dmadedate,
       v1.EnumName AS 有效期推算方式,
       a.cExpirationdate AS 有效期至,
       a.cciqbookcode AS 手册号,
       a.cbmemo AS 表体备注,
       vouchname.EnumName AS DJLX,
       a.cMemo AS BZ,
       a.cBusType AS YWLX,
       cBusCode AS ywh,
       W.cWhName AS WN,
       W.cWhCode AS WC,
       a.cCode AS DJH,
       Rd.cRdName AS SFLB,
       D.cDepName AS BM,
       P.cPersonName AS YWY,
       Pt.cPTName AS CGLX,
       (CASE
            WHEN ISNULL(V.cVenName, N'') = N'' THEN
                V.cVenAbbName
            ELSE
                V.cVenName
        END
       ) AS GHDW,
       DDH,
       DHDH,
       SaleType.cSTName AS XSLX,
       CONVERT(VARCHAR(30), a.cBillCode) AS FPH,
       (CASE
            WHEN ISNULL(C.cCusName, N'') = N'' THEN
                C.cCusAbbName
            ELSE
                C.cCusName
        END
       ) AS KH,
       CONVERT(VARCHAR(30), a.cDLCode) AS FHDH,
       ISNULL(a.cProBatch, N'') AS SCPH,
       a.cHandler AS 审核人,
       a.dVeriDate AS 审核日期,
       ISNULL(cDefine1, N'') AS CDEFINE1,
       ISNULL(cDefine14, N'') AS CDEFINE14,
       ISNULL(cDefine26, NULL) AS CDEFINE26,
       ISNULL(cDefine27, NULL) AS CDEFINE27,
       ISNULL(cDefine29, N'') AS CDEFINE29,
       ISNULL(cDefine31, N'') AS CDEFINE31,
       ISNULL(cDefine33, N'') AS CDEFINE33,
       ISNULL(cDefine35, NULL) AS CDEFINE35,
       ISNULL(cDefine36, NULL) AS CDEFINE36,
       ISNULL(cDefine37, NULL) AS CDEFINE37,
       cBatchProperty1,
       cBatchProperty2,
       cBatchProperty3,
       cBatchProperty4,
       cBatchProperty5,
       cBatchProperty6,
       cBatchProperty7,
       cBatchProperty8,
       cBatchProperty9,
       cBatchProperty10,
       a.cInvCode AS CHBM,
       i.cInvAddCode AS CHDM,
       i.cInvName AS CHMC,
       i.cInvStd AS GGXH,
       ISNULL(   (CASE
                      WHEN a.bRdFlag = 1 THEN
                          a.iNum
                      ELSE
                          CONVERT(DECIMAL(38, 6), NULL)
                  END
                 ),
                 0
             ) AS RKJS,
       ISNULL(   (CASE
                      WHEN a.bRdFlag = 0 THEN
                          a.iNum
                      ELSE
                          CONVERT(DECIMAL(38, 6), NULL)
                  END
                 ),
                 0
             ) AS CKJS,
       (CASE
            WHEN i.iGroupType = 0 THEN
                0
            WHEN i.iGroupType = 2 THEN
       (CASE
            WHEN a.iQuantity = 0
                 OR ISNULL(a.iNum, 0) = 0 THEN
                NULL
            ELSE
       (a.iQuantity / a.iNum)
        END
       )
            WHEN i.iGroupType = 1 THEN
                Unit.iChangRate
        END
       ) AS HSL,
       MUnit.cComUnitName AS JLDW,
       Unit.cComUnitName AS 库存单位,
       i.cInvDefine1,
       i.cInvDefine2,
       i.cInvDefine3,
       i.cInvDefine4,
       i.cInvDefine10,
       ISNULL(   (CASE
                      WHEN a.bRdFlag = 1 THEN
                          a.iQuantity
                      ELSE
                          CONVERT(DECIMAL(38, 6), NULL)
                  END
                 ),
                 0
             ) AS RKSL,
       ISNULL(   (CASE
                      WHEN a.bRdFlag = 0 THEN
                          a.iQuantity
                      ELSE
                          CONVERT(DECIMAL(38, 6), NULL)
                  END
                 ),
                 0
             ) AS CKSL,
       (CASE
            WHEN a.bRdFlag <> 0 THEN
                a.iUnitCost
            ELSE
                CONVERT(DECIMAL(38, 6), NULL)
        END
       ) AS RKDJ,
       (CASE
            WHEN a.bRdFlag <> 0 THEN
                CONVERT(DECIMAL(38, 6), NULL)
            ELSE
                a.iUnitCost
        END
       ) AS 出库单价,
       (CASE
            WHEN a.bRdFlag <> 0 THEN
                a.iPrice
            ELSE
                CONVERT(DECIMAL(38, 2), NULL)
        END
       ) AS 入库金额,
       (CASE
            WHEN a.bRdFlag <> 0 THEN
                CONVERT(DECIMAL(38, 2), NULL)
            ELSE
                a.iPrice
        END
       ) AS 出库金额,
       a.iPUnitCost AS 计划价,
       a.iPPrice AS 计划金额,
       PH,
       项目编码,
       XMMC,
       项目大类编码,
       XMDL,
       JZR,
       ZDR,
       检验员,
       检验单号,
       检验日期,
       CONVERT(VARCHAR(20), N'') AS 处理单号,
       ISNULL(vOrderType.EnumName, N'') AS XSDDLB,
       ISNULL(a.iordercode, N'') AS XSDDH,
       ISNULL(CONVERT(NVARCHAR(40), a.iorderseq), N'') AS xsddhh,
       委外订单号,
       生产订单号,
       币种,
       汇率,
       税率,
       原币无税单价,
       原币无税金额,
       原币税额,
       原币价税合计,
       本币税额,
       本币价税合计,
       ISNULL(   (CASE
                      WHEN a.bRdFlag <> 0 THEN
                          a.iNQuantity
                      ELSE
                          CONVERT(DECIMAL(38, 6), NULL)
                  END
                 ),
                 0
             ) AS 应收数量,
       ISNULL(   (CASE
                      WHEN a.bRdFlag <> 0 THEN
                          CONVERT(DECIMAL(38, 6), NULL)
                      ELSE
                          a.iNQuantity
                  END
                 ),
                 0
             ) AS 应发数量,
       ISNULL(   (CASE
                      WHEN a.bRdFlag <> 0 THEN
                          a.iNNum
                      ELSE
                          CONVERT(DECIMAL(38, 6), NULL)
                  END
                 ),
                 0
             ) AS 应收件数,
       ISNULL(   (CASE
                      WHEN a.bRdFlag <> 0 THEN
                          CONVERT(DECIMAL(38, 6), NULL)
                      ELSE
                          a.iNNum
                  END
                 ),
                 0
             ) AS 应发件数,
       i.cInvCCode AS CHFLBM,
       IC.cInvCName AS CHFLMC,
       ISNULL(vsotype.EnumName, N'') AS isotypename,
       a.csocode,
       CONVERT(NVARCHAR(40), a.isoseq) AS isoseq,
       (CASE
            WHEN a.isotype = 4 THEN
                AA.cRClassName
            ELSE
                N''
        END
       ) AS cdemandmemo,
       a.cVouchType AS cvouchtype,
       NULL AS cDefine2,
       NULL AS cDefine3,
       NULL AS cDefine4,
       NULL AS cDefine5,
       NULL AS cDefine6,
       NULL AS cDefine7,
       NULL AS cDefine8,
       NULL AS cDefine9,
       NULL AS cDefine10,
       NULL AS cFree1,
       NULL AS cFree2,
       NULL AS cDefine22,
       NULL AS cDefine23,
       NULL AS cDefine24,
       NULL AS cDefine25,
       NULL AS cFree3,
       NULL AS cFree4,
       NULL AS cFree5,
       NULL AS cFree6,
       NULL AS cFree7,
       NULL AS cFree8,
       NULL AS cFree9,
       NULL AS cFree10,
       NULL AS cDefine11,
       NULL AS cDefine12,
       NULL AS cDefine13,
       NULL AS cDefine15,
       NULL AS cDefine16,
       NULL AS cDefine28,
       NULL AS cDefine30,
       NULL AS cDefine32,
       NULL AS cDefine34,
       NULL AS cInvDefine5,
       NULL AS cInvDefine6,
       NULL AS cInvDefine7,
       NULL AS cInvDefine8,
       NULL AS cInvDefine9,
       NULL AS cInvDefine11,
       NULL AS cInvDefine12,
       NULL AS cInvDefine13,
       NULL AS cInvDefine14,
       NULL AS cInvDefine15,
       NULL AS cInvDefine16
FROM
( --销售出库单
    SELECT R.ID AS ID,
           R.dDate,
           Rs.AutoID,
           R.bRdFlag,
           Rs.dVDate,
           Rs.dMadeDate AS dmadedate,
           ISNULL(Rs.iExpiratDateCalcu, 0) AS iExpiratDateCalcu,
           Rs.cExpirationdate AS cExpirationdate,
           Rs.cciqbookcode,
           Rs.cbMemo AS cbmemo,
           R.cVouchType,
           R.cBusType,
           R.cMemo,
           (CASE
                WHEN R.cVouchType = N'32'
                     AND
                     (
                         ISNULL(R.cSource, N'') = N'发货单'
                         OR ISNULL(R.cSource, N'') = N'委托代销'
                     ) THEN
                    ISNULL(Rs.cbdlcode, N'')
                ELSE
                    ISNULL(R.cBusCode, N'')
            END
           ) AS cBusCode,
           R.cWhCode,
           R.cCode,
           R.cRdCode,
           R.cDepCode,
           R.cPersonCode,
           R.cPTCode,
           CONVERT(NVARCHAR(2), N'') AS DDH,
           CONVERT(NVARCHAR(2), N'') AS DHDH,
           R.cSTCode,
           R.cBillCode,
           R.cCusCode,
           R.cDLCode,
           R.cProBatch,
           R.cHandler,
           R.dVeriDate,
           ISNULL(R.cDefine1, N'') AS cDefine1,
           ISNULL(R.cDefine14, N'') AS cDefine14,
           ISNULL(Rs.cDefine26, NULL) AS cDefine26,
           ISNULL(Rs.cDefine27, NULL) AS cDefine27,
           ISNULL(Rs.cDefine29, N'') AS cDefine29,
           ISNULL(Rs.cDefine31, N'') AS cDefine31,
           ISNULL(Rs.cDefine33, N'') AS cDefine33,
           ISNULL(Rs.cDefine35, NULL) AS cDefine35,
           ISNULL(Rs.cDefine36, NULL) AS cDefine36,
           ISNULL(Rs.cDefine37, NULL) AS cDefine37,
           Rs.cBatchProperty1,
           Rs.cBatchProperty2,
           Rs.cBatchProperty3,
           Rs.cBatchProperty4,
           Rs.cBatchProperty5,
           Rs.cBatchProperty6,
           Rs.cBatchProperty7,
           Rs.cBatchProperty8,
           Rs.cBatchProperty9,
           Rs.cBatchProperty10,
           Rs.cInvCode AS cInvCode,
           Rs.iQuantity,
           Rs.iNum,
           Rs.iUnitCost,
           Rs.iPrice,
           Rs.iPUnitCost,
           Rs.iPPrice,
           Rs.cBatch AS PH,
           Rs.cItemCode AS 项目编码,
           Rs.cName AS XMMC,
           Rs.cItem_class AS 项目大类编码,
           Rs.cItemCName AS XMDL,
           Rs.cbaccounter AS JZR,
           R.cMaker AS ZDR,
           R.cChkPerson AS 检验员,
           R.cChkCode AS 检验单号,
           R.dChkDate AS 检验日期,
           Rs.iordertype,
           iordercode,
           Rs.iorderseq,
           NULL AS 委外订单号,
           CONVERT(NVARCHAR(60), N'') AS 生产订单号,
           NULL AS 币种,
           NULL AS 汇率,
           NULL AS 税率,
           NULL AS 原币无税单价,
           NULL AS 原币无税金额,
           NULL AS 原币税额,
           NULL AS 原币价税合计,
           NULL AS 本币税额,
           NULL AS 本币价税合计,
           Rs.iNQuantity,
           Rs.iNNum,
           Rs.isotype,
           Rs.csocode,
           Rs.isoseq,
           Rs.isodid,
           NULL AS cVenCode,
           Rs.cvmivencode,
           Rs.cAssUnit
    FROM rdrecord32 R WITH (NOLOCK)
        LEFT JOIN rdrecords32 Rs WITH (NOLOCK)
            ON R.ID = Rs.ID
        LEFT JOIN Warehouse w
            ON R.cWhCode = w.cWhCode
        LEFT JOIN Factory
            ON w.cFactoryCode = Factory.cFactoryCode
    --WHERE 1 = 1
    --      AND
    --      (
    --          (R.dDate >= N'2021-01-01')
    --          AND (R.dDate <= N'2021-12-31')
    --      )
    --      AND
    --      (
    --          (Rs.cInvCode >= N'CE02050030')
    --          AND (Rs.cInvCode <= N'CE02050030')
    --      )
    UNION ALL
    --材料出库单
    SELECT R.ID AS ID,
           R.dDate,
           Rs.AutoID,
           R.bRdFlag,
           Rs.dVDate,
           Rs.dMadeDate AS dmadedate,
           ISNULL(Rs.iExpiratDateCalcu, 0) AS iExpiratDateCalcu,
           Rs.cExpirationdate AS cExpirationdate,
           Rs.cciqbookcode,
           Rs.cbMemo AS cbmemo,
           R.cVouchType,
           R.cBusType,
           R.cMemo,
           ISNULL(R.cBusCode, N'') AS cBusCode,
           R.cWhCode,
           R.cCode,
           R.cRdCode,
           R.cDepCode,
           R.cPersonCode,
           R.cPTCode,
           CONVERT(NVARCHAR(2), N'') AS DDH,
           CONVERT(NVARCHAR(2), N'') AS DHDH,
           R.cSTCode,
           R.cBillCode,
           R.cCusCode,
           R.cDLCode,
           Rs.cMoLotCode AS cProBatch,
           R.cHandler,
           R.dVeriDate,
           ISNULL(R.cDefine1, N'') AS cDefine1,
           ISNULL(R.cDefine14, N'') AS cDefine14,
           ISNULL(Rs.cDefine26, NULL) AS cDefine26,
           ISNULL(Rs.cDefine27, NULL) AS cDefine27,
           ISNULL(Rs.cDefine29, N'') AS cDefine29,
           ISNULL(Rs.cDefine31, N'') AS cDefine31,
           ISNULL(Rs.cDefine33, N'') AS cDefine33,
           ISNULL(Rs.cDefine35, NULL) AS cDefine35,
           ISNULL(Rs.cDefine36, NULL) AS cDefine36,
           ISNULL(Rs.cDefine37, NULL) AS cDefine37,
           Rs.cBatchProperty1,
           Rs.cBatchProperty2,
           Rs.cBatchProperty3,
           Rs.cBatchProperty4,
           Rs.cBatchProperty5,
           Rs.cBatchProperty6,
           Rs.cBatchProperty7,
           Rs.cBatchProperty8,
           Rs.cBatchProperty9,
           Rs.cBatchProperty10,
           Rs.cInvCode AS cInvCode,
           Rs.iQuantity,
           Rs.iNum,
           Rs.iUnitCost,
           Rs.iPrice,
           Rs.iPUnitCost,
           Rs.iPPrice,
           Rs.cBatch AS PH,
           Rs.cItemCode AS 项目编码,
           Rs.cName AS XMMC,
           Rs.cItem_class AS 项目大类编码,
           Rs.cItemCName AS XMDL,
           Rs.cbaccounter AS JZR,
           R.cMaker AS ZDR,
           R.cChkPerson AS 检验员,
           R.cChkCode AS 检验单号,
           R.dChkDate AS 检验日期,
           Rs.iordertype,
           iordercode,
           Rs.iorderseq,
           Rs.comcode AS 委外订单号,
           ISNULL(cmocode, N'') AS 生产订单号,
           NULL AS 币种,
           NULL AS 汇率,
           NULL AS 税率,
           NULL AS 原币无税单价,
           NULL AS 原币无税金额,
           NULL AS 原币税额,
           NULL AS 原币价税合计,
           NULL AS 本币税额,
           NULL AS 本币价税合计,
           Rs.iNQuantity,
           Rs.iNNum,
           Rs.isotype,
           Rs.csocode,
           Rs.isoseq,
           Rs.isodid,
           R.cVenCode AS cVenCode,
           Rs.cvmivencode,
           Rs.cAssUnit
    FROM rdrecord11 R WITH (NOLOCK)
        LEFT JOIN rdrecords11 Rs WITH (NOLOCK)
            ON R.ID = Rs.ID
        LEFT JOIN Warehouse w
            ON R.cWhCode = w.cWhCode
        LEFT JOIN Factory
            ON w.cFactoryCode = Factory.cFactoryCode
    UNION ALL
    --产成品入库单
    SELECT R.ID AS ID,
           R.dDate,
           Rs.AutoID,
           R.bRdFlag,
           Rs.dVDate,
           Rs.dMadeDate AS dmadedate,
           ISNULL(Rs.iExpiratDateCalcu, 0) AS iExpiratDateCalcu,
           Rs.cExpirationdate AS cExpirationdate,
           Rs.cciqbookcode,
           Rs.cbMemo AS cbmemo,
           R.cVouchType,
           R.cBusType,
           R.cMemo,
           ISNULL(R.cBusCode, N'') AS cBusCode,
           R.cWhCode,
           R.cCode,
           R.cRdCode,
           R.cDepCode,
           R.cPersonCode,
           R.cPTCode,
           CONVERT(NVARCHAR(2), N'') AS DDH,
           CONVERT(NVARCHAR(2), N'') AS DHDH,
           R.cSTCode,
           R.cBillCode,
           R.cCusCode,
           R.cDLCode,
           Rs.cMoLotCode AS cProBatch,
           R.cHandler,
           R.dVeriDate,
           ISNULL(R.cDefine1, N'') AS cDefine1,
           ISNULL(R.cDefine14, N'') AS cDefine14,
           ISNULL(Rs.cDefine26, NULL) AS cDefine26,
           ISNULL(Rs.cDefine27, NULL) AS cDefine27,
           ISNULL(Rs.cDefine29, N'') AS cDefine29,
           ISNULL(Rs.cDefine31, N'') AS cDefine31,
           ISNULL(Rs.cDefine33, N'') AS cDefine33,
           ISNULL(Rs.cDefine35, NULL) AS cDefine35,
           ISNULL(Rs.cDefine36, NULL) AS cDefine36,
           ISNULL(Rs.cDefine37, NULL) AS cDefine37,
           Rs.cBatchProperty1,
           Rs.cBatchProperty2,
           Rs.cBatchProperty3,
           Rs.cBatchProperty4,
           Rs.cBatchProperty5,
           Rs.cBatchProperty6,
           Rs.cBatchProperty7,
           Rs.cBatchProperty8,
           Rs.cBatchProperty9,
           Rs.cBatchProperty10,
           Rs.cInvCode AS cInvCode,
           Rs.iQuantity,
           Rs.iNum,
           Rs.iUnitCost,
           Rs.iPrice,
           Rs.iPUnitCost,
           Rs.iPPrice,
           Rs.cBatch AS PH,
           Rs.cItemCode AS 项目编码,
           Rs.cName AS XMMC,
           Rs.cItem_class AS 项目大类编码,
           Rs.cItemCName AS XMDL,
           Rs.cbaccounter AS JZR,
           R.cMaker AS ZDR,
           R.cChkPerson AS 检验员,
           R.cChkCode AS 检验单号,
           R.dChkDate AS 检验日期,
           Rs.iordertype,
           iordercode,
           Rs.iorderseq,
           NULL AS 委外订单号,
           ISNULL(cmocode, N'') AS 生产订单号,
           NULL AS 币种,
           NULL AS 汇率,
           NULL AS 税率,
           NULL AS 原币无税单价,
           NULL AS 原币无税金额,
           NULL AS 原币税额,
           NULL AS 原币价税合计,
           NULL AS 本币税额,
           NULL AS 本币价税合计,
           Rs.iNQuantity,
           Rs.iNNum,
           Rs.isotype,
           Rs.csocode,
           Rs.isoseq,
           Rs.isodid,
           NULL AS cVenCode,
           Rs.cvmivencode,
           Rs.cAssUnit
    FROM rdrecord10 R WITH (NOLOCK)
        LEFT JOIN rdrecords10 Rs WITH (NOLOCK)
            ON R.ID = Rs.ID
        LEFT JOIN Warehouse w
            ON R.cWhCode = w.cWhCode
        LEFT JOIN Factory
            ON w.cFactoryCode = Factory.cFactoryCode
    UNION ALL
    ---其他出库单
    SELECT R.ID AS ID,
           R.dDate,
           Rs.AutoID,
           R.bRdFlag,
           Rs.dVDate,
           Rs.dMadeDate AS dmadedate,
           ISNULL(Rs.iExpiratDateCalcu, 0) AS iExpiratDateCalcu,
           Rs.cExpirationdate AS cExpirationdate,
           Rs.cciqbookcode,
           Rs.cbMemo AS cbmemo,
           R.cVouchType,
           R.cBusType,
           R.cMemo,
           ISNULL(R.cBusCode, N'') AS cBusCode,
           R.cWhCode,
           R.cCode,
           R.cRdCode,
           R.cDepCode,
           R.cPersonCode,
           R.cPTCode,
           CONVERT(NVARCHAR(2), N'') AS DDH,
           CONVERT(NVARCHAR(2), N'') AS DHDH,
           R.cSTCode,
           R.cBillCode,
           R.cCusCode,
           R.cDLCode,
           R.cProBatch,
           R.cHandler,
           R.dVeriDate,
           ISNULL(R.cDefine1, N'') AS cDefine1,
           ISNULL(R.cDefine14, N'') AS cDefine14,
           ISNULL(Rs.cDefine26, NULL) AS cDefine26,
           ISNULL(Rs.cDefine27, NULL) AS cDefine27,
           ISNULL(Rs.cDefine29, N'') AS cDefine29,
           ISNULL(Rs.cDefine31, N'') AS cDefine31,
           ISNULL(Rs.cDefine33, N'') AS cDefine33,
           ISNULL(Rs.cDefine35, NULL) AS cDefine35,
           ISNULL(Rs.cDefine36, NULL) AS cDefine36,
           ISNULL(Rs.cDefine37, NULL) AS cDefine37,
           Rs.cBatchProperty1,
           Rs.cBatchProperty2,
           Rs.cBatchProperty3,
           Rs.cBatchProperty4,
           Rs.cBatchProperty5,
           Rs.cBatchProperty6,
           Rs.cBatchProperty7,
           Rs.cBatchProperty8,
           Rs.cBatchProperty9,
           Rs.cBatchProperty10,
           Rs.cInvCode AS cInvCode,
           Rs.iQuantity,
           Rs.iNum,
           Rs.iUnitCost,
           Rs.iPrice,
           Rs.iPUnitCost,
           Rs.iPPrice,
           Rs.cBatch AS PH,
           Rs.cItemCode AS 项目编码,
           Rs.cName AS XMMC,
           Rs.cItem_class AS 项目大类编码,
           Rs.cItemCName AS XMDL,
           Rs.cbaccounter AS JZR,
           R.cMaker AS ZDR,
           R.cChkPerson AS 检验员,
           R.cChkCode AS 检验单号,
           R.dChkDate AS 检验日期,
           Rs.iordertype,
           iordercode,
           Rs.iorderseq,
           NULL AS 委外订单号,
           CONVERT(NVARCHAR(60), N'') AS 生产订单号,
           NULL AS 币种,
           NULL AS 汇率,
           NULL AS 税率,
           NULL AS 原币无税单价,
           NULL AS 原币无税金额,
           NULL AS 原币税额,
           NULL AS 原币价税合计,
           NULL AS 本币税额,
           NULL AS 本币价税合计,
           Rs.iNQuantity,
           Rs.iNNum,
           Rs.isotype,
           Rs.csocode,
           Rs.isoseq,
           Rs.isodid,
           NULL AS cVenCode,
           Rs.cvmivencode,
           Rs.cAssUnit
    FROM RdRecord09 R WITH (NOLOCK)
        LEFT JOIN rdrecords09 Rs WITH (NOLOCK)
            ON R.ID = Rs.ID
        LEFT JOIN Warehouse w
            ON R.cWhCode = w.cWhCode
        LEFT JOIN Factory
            ON w.cFactoryCode = Factory.cFactoryCode
    UNION ALL
    --其他入库单
    SELECT R.ID AS ID,
           R.dDate,
           Rs.AutoID,
           R.bRdFlag,
           Rs.dVDate,
           Rs.dMadeDate AS dmadedate,
           ISNULL(Rs.iExpiratDateCalcu, 0) AS iExpiratDateCalcu,
           Rs.cExpirationdate AS cExpirationdate,
           Rs.cciqbookcode,
           Rs.cbMemo AS cbmemo,
           R.cVouchType,
           R.cBusType,
           R.cMemo,
           ISNULL(R.cBusCode, N'') AS cBusCode,
           R.cWhCode,
           R.cCode,
           R.cRdCode,
           R.cDepCode,
           R.cPersonCode,
           R.cPTCode,
           CONVERT(NVARCHAR(2), N'') AS DDH,
           CONVERT(NVARCHAR(2), N'') AS DHDH,
           R.cSTCode,
           R.cBillCode,
           R.cCusCode,
           R.cDLCode,
           R.cProBatch,
           R.cHandler,
           R.dVeriDate,
           ISNULL(R.cDefine1, N'') AS cDefine1,
           ISNULL(R.cDefine14, N'') AS cDefine14,
           ISNULL(Rs.cDefine26, NULL) AS cDefine26,
           ISNULL(Rs.cDefine27, NULL) AS cDefine27,
           ISNULL(Rs.cDefine29, N'') AS cDefine29,
           ISNULL(Rs.cDefine31, N'') AS cDefine31,
           ISNULL(Rs.cDefine33, N'') AS cDefine33,
           ISNULL(Rs.cDefine35, NULL) AS cDefine35,
           ISNULL(Rs.cDefine36, NULL) AS cDefine36,
           ISNULL(Rs.cDefine37, NULL) AS cDefine37,
           Rs.cBatchProperty1,
           Rs.cBatchProperty2,
           Rs.cBatchProperty3,
           Rs.cBatchProperty4,
           Rs.cBatchProperty5,
           Rs.cBatchProperty6,
           Rs.cBatchProperty7,
           Rs.cBatchProperty8,
           Rs.cBatchProperty9,
           Rs.cBatchProperty10,
           Rs.cInvCode AS cInvCode,
           Rs.iQuantity,
           Rs.iNum,
           Rs.iUnitCost,
           Rs.iPrice,
           Rs.iPUnitCost,
           Rs.iPPrice,
           Rs.cBatch AS PH,
           Rs.cItemCode AS 项目编码,
           Rs.cName AS XMMC,
           Rs.cItem_class AS 项目大类编码,
           Rs.cItemCName AS XMDL,
           Rs.cbaccounter AS JZR,
           R.cMaker AS ZDR,
           R.cChkPerson AS 检验员,
           R.cChkCode AS 检验单号,
           R.dChkDate AS 检验日期,
           Rs.iordertype,
           iordercode,
           Rs.iorderseq,
           NULL AS 委外订单号,
           CONVERT(NVARCHAR(60), N'') AS 生产订单号,
           NULL AS 币种,
           NULL AS 汇率,
           NULL AS 税率,
           NULL AS 原币无税单价,
           NULL AS 原币无税金额,
           NULL AS 原币税额,
           NULL AS 原币价税合计,
           NULL AS 本币税额,
           NULL AS 本币价税合计,
           Rs.iNQuantity,
           Rs.iNNum,
           Rs.isotype,
           Rs.csocode,
           Rs.isoseq,
           Rs.isodid,
           (CASE
                WHEN ISNULL(R.cBusType, N'') = N'调拨入库' THEN
                    Rs.cBVencode
                ELSE
                    R.cVenCode
            END
           ) AS cVenCode,
           Rs.cvmivencode,
           Rs.cAssUnit
    FROM RdRecord08 R WITH (NOLOCK)
        LEFT JOIN rdrecords08 Rs WITH (NOLOCK)
            ON R.ID = Rs.ID
        LEFT JOIN Warehouse w
            ON R.cWhCode = w.cWhCode
        LEFT JOIN Factory
            ON w.cFactoryCode = Factory.cFactoryCode
    UNION ALL
    --采购入库单
    SELECT R.ID AS ID,
           R.dDate,
           Rs.AutoID,
           R.bRdFlag,
           Rs.dVDate,
           Rs.dMadeDate AS dmadedate,
           ISNULL(Rs.iExpiratDateCalcu, 0) AS iExpiratDateCalcu,
           Rs.cExpirationdate AS cExpirationdate,
           Rs.cciqbookcode,
           Rs.cbMemo AS cbmemo,
           R.cVouchType,
           R.cBusType,
           R.cMemo,
           ISNULL(R.cBusCode, N'') AS cBusCode,
           R.cWhCode,
           R.cCode,
           R.cRdCode,
           R.cDepCode,
           R.cPersonCode,
           R.cPTCode,
           CASE
               WHEN ISNULL(Rs.iPOsID, 0) <> 0 THEN
                   Rs.cPOID
               ELSE
                   CONVERT(NVARCHAR(2), N'')
           END AS DDH,
           ISNULL(Rs.cbarvcode, CONVERT(NVARCHAR(2), N'')) AS DHDH,
           R.cSTCode,
           R.cBillCode,
           R.cCusCode,
           R.cDLCode,
           R.cProBatch,
           R.cHandler,
           R.dVeriDate,
           ISNULL(R.cDefine1, N'') AS cDefine1,
           ISNULL(R.cDefine14, N'') AS cDefine14,
           ISNULL(Rs.cDefine26, NULL) AS cDefine26,
           ISNULL(Rs.cDefine27, NULL) AS cDefine27,
           ISNULL(Rs.cDefine29, N'') AS cDefine29,
           ISNULL(Rs.cDefine31, N'') AS cDefine31,
           ISNULL(Rs.cDefine33, N'') AS cDefine33,
           ISNULL(Rs.cDefine35, NULL) AS cDefine35,
           ISNULL(Rs.cDefine36, NULL) AS cDefine36,
           ISNULL(Rs.cDefine37, NULL) AS cDefine37,
           Rs.cBatchProperty1,
           Rs.cBatchProperty2,
           Rs.cBatchProperty3,
           Rs.cBatchProperty4,
           Rs.cBatchProperty5,
           Rs.cBatchProperty6,
           Rs.cBatchProperty7,
           Rs.cBatchProperty8,
           Rs.cBatchProperty9,
           Rs.cBatchProperty10,
           Rs.cInvCode AS cInvCode,
           Rs.iQuantity,
           Rs.iNum,
           Rs.iUnitCost,
           Rs.iPrice,
           Rs.iPUnitCost,
           Rs.iPPrice,
           Rs.cBatch AS PH,
           Rs.cItemCode AS 项目编码,
           Rs.cName AS XMMC,
           Rs.cItem_class AS 项目大类编码,
           Rs.cItemCName AS XMDL,
           Rs.cbaccounter AS JZR,
           R.cMaker AS ZDR,
           CASE
               WHEN R.cSource = N'入库验收单' THEN
                   Rs.cCheckPersonCode
               ELSE
                   R.cChkPerson
           END AS 检验员,
           CASE
               WHEN R.cSource = N'入库验收单' THEN
                   Rs.cCheckCode
               ELSE
                   R.cChkCode
           END AS 检验单号,
           CASE
               WHEN R.cSource = N'入库验收单' THEN
                   Rs.dCheckDate
               ELSE
                   R.dChkDate
           END AS 检验日期,
           Rs.iordertype,
           iordercode,
           Rs.iorderseq,
           (CASE
                WHEN ISNULL(R.cVouchType, N'') = N'01'
                     AND ISNULL(R.cBusType, N'') = N'委外加工' THEN
                    Rs.cPOID
                ELSE
                    Rs.comcode
            END
           ) AS 委外订单号,
           CONVERT(NVARCHAR(60), N'') AS 生产订单号,
           R.cExch_Name AS 币种,
           R.iExchRate AS 汇率,
           R.iTaxRate AS 税率,
           Rs.iOriCost AS 原币无税单价,
           Rs.iOriMoney AS 原币无税金额,
           Rs.iOriTaxPrice AS 原币税额,
           Rs.ioriSum AS 原币价税合计,
           Rs.iTaxPrice AS 本币税额,
           Rs.iSum AS 本币价税合计,
           Rs.iNQuantity,
           Rs.iNNum,
           Rs.isotype,
           Rs.csocode,
           Rs.isoseq,
           Rs.isodid,
           R.cVenCode AS cVenCode,
           Rs.cvmivencode,
           Rs.cAssUnit
    FROM RdRecord01 R WITH (NOLOCK)
        LEFT JOIN rdrecords01 Rs WITH (NOLOCK)
            ON R.ID = Rs.ID
        LEFT JOIN Warehouse w
            ON R.cWhCode = w.cWhCode
        LEFT JOIN Factory
            ON w.cFactoryCode = Factory.cFactoryCode
) a
    INNER JOIN
    (
        SELECT cInvCode,
               cInvName,
               cInvAddCode,
               cInvStd,
               cInvDefine1,
               cInvDefine2,
               cInvDefine3,
               cInvDefine4,
               cInvDefine5,
               cInvDefine6,
               cInvDefine7,
               cInvDefine8,
               cInvDefine9,
               cInvDefine10,
               cInvDefine11,
               cInvDefine12,
               cInvDefine13,
               cInvDefine14,
               cInvDefine15,
               cInvDefine16,
               i.cInvCCode,
               ic.cInvCName,
               i.iGroupType,
               cComUnitCode,
               iId
        FROM Inventory i
            INNER JOIN InventoryClass ic
                ON i.cInvCCode = ic.cInvCCode
    --WHERE   (i.cInvCode >= N'CE02050030')
    --      AND (i.cInvCode <= N'CE02050030')
    ) i
        ON a.cInvCode = i.cInvCode
    LEFT JOIN Warehouse W
        ON W.cWhCode = a.cWhCode
    LEFT JOIN InventoryClass IC
        ON i.cInvCCode = IC.cInvCCode
    LEFT JOIN v_aa_enum vouchname
        ON vouchname.EnumCode = a.cVouchType
           AND vouchname.EnumType = N'ST.VouchType'
    LEFT JOIN Customer C
        ON C.cCusCode = a.cCusCode
    LEFT JOIN Department D
        ON D.cDepCode = a.cDepCode
    LEFT JOIN SaleType
        ON SaleType.cSTCode = a.cSTCode
    LEFT JOIN Vendor V
        ON V.cVenCode = a.cVenCode
    LEFT JOIN Vendor Ven
        ON Ven.cVenCode = a.cvmivencode
    LEFT OUTER JOIN Rd_Style Rd
        ON a.cRdCode = Rd.cRdCode
    LEFT OUTER JOIN PurchaseType Pt
        ON a.cPTCode = Pt.cPTCode
    LEFT OUTER JOIN Person P
        ON a.cPersonCode = P.cPersonCode
    LEFT JOIN ComputationUnit AS Unit
        ON a.cAssUnit = Unit.cComunitCode
    LEFT JOIN ComputationUnit AS MUnit
        ON i.cComUnitCode = MUnit.cComunitCode
    LEFT JOIN AA_RequirementClass AA
        ON AA.cRClassCode = a.isodid
           AND a.isotype = 4
    LEFT JOIN v_aa_enum v1
        ON ISNULL(a.iExpiratDateCalcu, 0) = v1.EnumCode
           AND v1.EnumType = N'SCM.ExpiratDateCalcu'
    LEFT JOIN v_aa_enum vsotype
        ON vsotype.EnumCode = a.isotype
           AND vsotype.EnumType = N'ST.Sotype'
    LEFT JOIN v_aa_enum vOrderType
        ON vOrderType.EnumCode = a.iordertype
           AND vOrderType.EnumType = N'ST.OrderType'
           --时间条件
           WHERE       (
          (a.dDate >= N'2021-01-01')
          AND (a.dDate <= N'2021-12-31')
      )
ORDER BY a.dDate

你可能感兴趣的:(用友,sql,数据库,运维)