INSE语句中的逻辑问题

时间:2017-11-02 18:58:01

标签: sql-server insert case

我正在向一个表格写一个INSERT语句,该表格包含基于"瀑布"分配给各种材料(文章)的员工姓名。逻辑,如果你愿意。基本上,我希望下面的CASE语句设置赋值规则。例如,如果PO类型为05且物料的库存类别为LIKE A60383%,则在不满足这些条件的情况下将PO分配给AZ,然后继续下一个逻辑文本。这种方法工作正常,除了多行PO的情况。它似乎只为第一行正确分配 - 下一行它使用语句下面的逻辑,特别是业务单元逻辑。业务单元逻辑已正确分配,但我希望第一条规则成为两条线的先例。知道我可能做错了什么吗?

以下是输出的示例 - 第一个员工分配是正确的,因为股票类别是A60385001,然后是MB,第二行不是遵循该逻辑而是分配给JLM,因为这篇文章在她的业务部门,但逻辑进一步下降。导致此PO在报告中显示两次。

PO# Qty Article Description PO Created BY   BU  Date    Assigned Employee
282859618   109 CF0864  YG ZNE CH FZ HD LANGEJUL    App Kids    8/31/2017 0:00  MB
282859618   191 CF0864  YG ZNE CH FZ HD LANGEJUL    App Kids    8/31/2017 0:00  JLM


INSERT INTO SCM_PO_EMPLOYEE_NAME (PO_NUMBER, PO_ITEM_NUMBER, MATERIAL,PO_TYPE,BUSINESS_UNIT_CODE,TEAM_MEMBER_NAME)



      SELECT DISTINCT I.PO_NUMBER, 
                                                      I.PO_ITEM_NUMBER, 
                                                      I.MATERIAL,
                                                      H.PO_TYPE,
                                                      B.BU_CODE,

                                             CASE WHEN H.PO_TYPE IN ('05','07') -- Promo PO type - should be on both po type and stock category
                                                  AND  I.STOCK_CATEGORY LIKE ('A60383%') -- verify the stock category on the PROMO PO is in 3000
                                                  THEN 'AZ'
                                                  WHEN I.STOCK_CATEGORY = 'A60385001' -- retail to Michelle
                                                  THEN 'MB'
                                                  WHEN H.PO_TYPE = '02' -- miadidas PO type
                                                  THEN 'MB'
                                                  WHEN H.PO_TYPE = '03' -- B grade PO
                                                  THEN 'MH'
                                                  WHEN H.PO_TYPE = 'DB' -- dummy PO
                                                  THEN 'JLM'
                                                  WHEN H.PO_TYPE = '12'-- ISC -- Can be called out on the article level?
                                                  AND  M.DEPARTMENT = '01'-- division is ftw
                                                  THEN 'MZ'
                                                  WHEN H.PO_TYPE = '12'-- ISC
                                                  AND  M.DEPARTMENT = '02' -- division is app
                                                  THEN 'JLM'
                                                  WHEN I.MATERIAL IN ( SELECT ARTICLE
                                                                       FROM   ADI_USER_MAINTAINED.dbo.SCM_EMPLOYEE_ARTICLE A ) -- Check the Employee to article table next
                                                  THEN A.TEAM_MEMBER_NAME -- If the article is in the table then assign the employee from the employee to article table
                                                  WHEN M.BUSINESS_UNIT_CODE = B.BU_CODE -- if not use then go to the BU assignment (below)
                                                  THEN B.TEAM_MEMBER_NAME  -- Use the team member name from the Employee_BU table
                                                  ELSE 'NO_EMPLOYEE' -- check for misses
                                             END  AS   [TEAM_MEMBER_NAME]

                                      FROM   PDX_SAP_USER.dbo.VW_PO_ITEM I 
                                      LEFT JOIN   PDX_SAP_USER.dbo.VW_PO_HEADER H ON I.PO_NUMBER = H.PO_NUMBER 
                                      LEFT JOIN   PDX_SAP_USER.dbo.VW_MM_MATERIAL M ON I.MATERIAL = M.MATERIAL 
                                      LEFT JOIN   ADI_USER_MAINTAINED.dbo.SCM_EMPLOYEE_ARTICLE A ON I.MATERIAL = A.ARTICLE
                                      LEFT JOIN   ADI_USER_MAINTAINED.dbo.SCM_EMPLOYEE_BU B ON B.BU_CODE = M.BUSINESS_UNIT_CODE

                                      WHERE H.VENDOR_NO <> '20800' --Exclude '20800' as a vendor!!
                                      AND   H.PO_TYPE <> '01';

0 个答案:

没有答案