子查询以映射商品

时间:2019-03-15 04:20:32

标签: sql sql-server

团队-编码的主要目的是映射客户购买保修后发送的部件,并将其与发送相同部件或发送不同部件的后续失败进行比较。

Warranty Replacement Part

Customer Purchase Information

我们正在尝试根据资产标签-他进行了多少购买以及正在发送什么商品来识别。

但是我们遇到了问题-比较原始订单和后续保修发货中发送的零件时。查询的日期不是最近的购买日期,而是最后的购买日期,并带有保修单的映射-导致错误的投影。

我尝试使用以下代码

,[Original Order] = (select a.PN_Combo from CTE3 b with (nolock) where a.ASST_ID = b.ASST_ID and a.[Number Row] = 1)

[B2C Create Date] = (select a.[DSPCH_CRT_DT] from CTE3 b where a.ASST_ID = b.ASST_ID and a.[Number Row] = 1 )

但是我收到一条错误消息

子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

我的需求是-查询应选择最新购买的商品以映射到保修单。

     With CTE1 As
(
SELECT distinct
c.[FISCAL_YEAR]
,c.[FISCAL_Quarter]
,c.[FISCAL_Month]
,c.[FISCAL_Week]
,a.[SVC_DSPCH_ID]
,f.[B2C_FLG]
,f.[B2D_FLG]
,d.[RGN_DESC]
,d.[SUB_RGN_DESC]
,d.[AREA_DESC]
,d.[CTRY_DESC]
,e.[LOB_DESC]
,e.[PROD_LN_DESC]
,a.[SVC_BU_ID]
,a.[SVC_ACTN_CD]
,a.[CURR_STAT]
,a.[ASST_ID]
,a.[SYS_ITM_CLS_CD]
,a.[CUST_NBR]
,a.[DSPCH_CRT_DT]
,a.[ORIG_ORD_NBR]
,a.[ORIG_ORD_BU_ID]
,a.[PROB_DESC]
,a.[CRT_SRC_PRSN_HIST_GEN_ID]
,f.[REPEAT_TM_GAP_SECND] / 86400 as 'REPEAT_TM_GAP_DAY'
,f.[REPEAT_DSPCH_NBR]
,a.[TMZN_LOC_ID]
,b.[BUS_RPTG_TEAM_NM]
,b.[ASSOC_FULL_NM]
,g.[CONTRACT_ACTIVE]

      FROM [RDATA].[S_P].[G_D_F] a With (Nolock) LEFT OUTER JOIN [RD].[P_P].[A_D] b With (Nolock)
      on
      a.[CRT_SRC_PRSN_HIST_GEN_ID]=b.[SRC_PRSN_HIST_GEN_ID]
      Left Outer Join [RD].[C_WW].[F_D_C] C With (Nolock)
      on a.[DSPCH_CRT_DT] = c.[ACTUAL_DATE]
      left outer join [RD].[C_P].[P_G_H_D_V] d With (Nolock)
      on a.[SVC_BU_ID] = d.[BU_ID]
      left outer join [RD].[I_P].[B_P_H_D] e With (Nolock)
      on
      a.[SYS_ITM_CLS_CD] = e.[ITM_CLS_CODE]
      left outer join [RD].[S_P].[G_M_R_F] f With (Nolock)
      on
      a.[SVC_DSPCH_ID] = f.[SVC_DSPCH_ID]
      left outer join [GD].[o].[IB g With (Nolock)
      on a.[ASST_ID] = g.[ASST_ID]


  where [SVC_ACTN_CD] in ('OWCN','OW5','OWPCN','OWP','BILCIS3BD(RC)','BILCIS6BD(RC)','BILCIS4BD(RC)','POR','PABILCAR','BILMIS')
  and a.[SVC_DSPCH_ID] is not null
  and [CURR_STAT] not in ('Cancelled','Cancellation Request','CANCELED','REJECTED','Problem')
  and d.[SUB_RGN_DESC] != 'Japan / Korea'
  and f.[B2C_FLG] = 1
  and c.[FISCAL_Week] > = '201901'
  and a.[TMZN_LOC_ID] in ('3','4')

     )
  , CTE2 As
  (
  Select distinct a.*
  ,Stuff( (
  select  ','+' '+ [COMMODITY_DESC]
  from CTE1 b left outer join [RD].[S_P].[G_S_D_I_O_D] c
  on b.[SVC_DSPCH_ID] = c.[SVC_DSPCH_ID]
  left outer join [RD].[S].[C_M_I] d
  on c.[ITM_NBR] = d.[ITEM_NUM]
  left outer join [RD].[C_].[R_C] e
  on
  d.[REPORTING_COMMODITY_ID] = e.[REPORTING_COMMODITY_ID]
  where b.[SVC_DSPCH_ID] = a.[SVC_DSPCH_ID]
  order by
  e.[COMMODITY_DESC] desc
  FOR XML PATH('')), 1, LEN(','), '') as 'PN_Combo'

   from CTE1 a    
   )
, CTE2A as
(
Select distinct *
,row_number() over(partition by a.[ASST_ID] order by a.[DSPCH_CRT_DT] desc) as 'Number Row'
from CTE2 a
)
,CTE3 AS
(
select 
 a.[FISCAL_YEAR]
,a.[FISCAL_Quarter]
,a.[FISCAL_Month]
,a.[FISCAL_Week]
,a.[SVC_DSPCH_ID]
,a.[B2C_FLG]
,a.[B2D_FLG]
,a.[RGN_DESC]
,a.[SUB_RGN_DESC]
,a.[AREA_DESC]
,a.[CTRY_DESC]
,a.[LOB_DESC]
,a.[PROD_LN_DESC]
,a.[SVC_BU_ID]
,a.[SVC_ACTN_CD]
,a.[CURR_STAT]
,a.[ASST_ID]
,a.[SYS_ITM_CLS_CD]
,a.[CUST_NBR]
,a.[DSPCH_CRT_DT]
,a.[ORIG_ORD_NBR]
,a.[ORIG_ORD_BU_ID]
,a.[PROB_DESC]
,a.[Number Row]
,a.[PN_Combo]
,a.[CRT_SRC_PRSN_HIST_GEN_ID]
,a.[REPEAT_TM_GAP_DAY]
,a.[REPEAT_DSPCH_NBR]
,a.[TMZN_LOC_ID]
,a.[BUS_RPTG_TEAM_NM]
,a.[ASSOC_FULL_NM]
,a.[CONTRACT_ACTIVE]


from CTE2A A

group by
a.[FISCAL_YEAR]
,a.[FISCAL_Quarter]
,a.[FISCAL_Month]
,a.[FISCAL_Week]
,a.[SVC_DSPCH_ID]
,a.[B2C_FLG]
,a.[B2D_FLG]
,a.[RGN_DESC]
,a.[SUB_RGN_DESC]
,a.[AREA_DESC]
,a.[CTRY_DESC]
,a.[LOB_DESC]
,a.[PROD_LN_DESC]
,a.[SVC_BU_ID]
,a.[SVC_ACTN_CD]
,a.[CURR_STAT]
,a.[ASST_ID]
,a.[SYS_ITM_CLS_CD]
,a.[CUST_NBR]
,a.[DSPCH_CRT_DT]
,a.[ORIG_ORD_NBR]
,a.[ORIG_ORD_BU_ID]
,a.[PROB_DESC]
,a.[CRT_SRC_PRSN_HIST_GEN_ID]
,a.[REPEAT_TM_GAP_DAY]
,a.[REPEAT_DSPCH_NBR]
,a.[TMZN_LOC_ID]
,a.[BUS_RPTG_TEAM_NM]
,a.[ASSOC_FULL_NM]
,a.[CONTRACT_ACTIVE]
,a.[Number Row]
,a.[PN_Combo]
)
,Dispatch_Map AS
  (
  select distinct
   d.[FISCAL_YEAR]
  ,d.[FISCAL_Quarter]
  ,d.[FISCAL_Month]
  ,d.[FISCAL_Week]
  ,a.[RGN_DESC]
  ,a.[SUB_RGN_DESC]
  ,a.[AREA_DESC]
  ,a.[CTRY_DESC]
  ,a.[LOB_DESC]
  ,a.[PROD_LN_DESC]
  ,a.[ASST_ID]
  ,b.[SVC_DSPCH_ID]
  ,b.[X_ISP_REPEAT_REAS_VAL]
  ,[Original Order] = (select a.PN_Combo from CTE3 b with (nolock) where a.ASST_ID = b.ASST_ID and a.[Number Row] = 1)
  ,b.[Dispatch_Part_Commodity_List] 
  ,b.[DSPCH_CRT_DT]
  ,[B2C Create Date] = (select a.[DSPCH_CRT_DT] from CTE3 b where a.ASST_ID = b.ASST_ID and a.[Number Row] = 1 )
  ,b.[SVC_ACTN_CD]
  ,c.[BUS_RPTG_TEAM_NM]
  ,c.[ASSOC_FULL_NM]
  ,DATEDIFF(day,lag(a.[DSPCH_CRT_DT],0) over (Partition by a.[ASST_ID] Order by a.[DSPCH_CRT_DT] desc),b.[DSPCH_CRT_DT]) as 'Duration'
  ,Sum (e.[ITM_COST]) over (Partition by b.[SVC_DSPCH_ID]) as 'Total Cost'
  ,ROW_NUMBER() OVER(Partition by b.[SVC_DSPCH_ID] ORDER BY b.[SVC_DSPCH_ID] ) AS 'Row_Count'
  ,Case 
  When a.[SVC_ACTN_CD] in ('OWCN','OW5') then 'P&L'
  When a.[SVC_ACTN_CD] in ('OWPCN','OWP') then 'Parts only'
  When a.[SVC_ACTN_CD] in ('BILCIS3BD(RC)','BILCIS6BD(RC)','BILCIS4BD(RC)') then 'CIS'
  When a.[SVC_ACTN_CD] in ('POR','PABILCAR') then 'CAR'
  When a.[SVC_ACTN_CD] in ('BILMIS') then 'MIS'
  Else 'Other' End as 'GSD Tower'
  ,Case
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Miscellaneous' and b.[Dispatch_Part_Commodity_List] = 'Motherboard' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'LCD Panel' and b.[Dispatch_Part_Commodity_List] = 'LCD' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation' and b.[Dispatch_Part_Commodity_List] = 'Motherboard' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation, Battery (Notebook)' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, LCD Panel, Documentation' and b.[Dispatch_Part_Commodity_List] = 'LCD Panel' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Memory' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Keyboard, Documentation' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation, Battery (Notebook)' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation' and b.[Dispatch_Part_Commodity_List] = 'Motherboard(2), Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation, Chassis' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'LCD Panel, Chassis, Chassis' and b.[Dispatch_Part_Commodity_List] = 'LCD Panel, Chassis(2)' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation, Chassis' and b.[Dispatch_Part_Commodity_List] = 'Motherboard' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Miscellaneous, Mechanical Hardware, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Motherboard, Documentation, Documentation' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Fan Assembly, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Miscellaneous, Mechanical Hardware, Documentation, Cables' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Miscellaneous, Mechanical Hardware, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Miscellaneous, Mechanical Hardware, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Miscellaneous, Mechanical Hardware, Keyboard, Chassis, Chassis, Chassis, Cables' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Miscellaneous, Mechanical Hardware, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Documentation, Battery (Notebook)' and b.[Dispatch_Part_Commodity_List] = 'Motherboard' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) = 'Motherboard, Miscellaneous, Mechanical Hardware, Documentations' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) like '%Motherboard, Documentation%' and b.[Dispatch_Part_Commodity_List] = 'Motherboard, Documentation' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) like '%Hard Drive%' and b.[Dispatch_Part_Commodity_List] = 'Hard Drive' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) like '%Chassis%' and b.[Dispatch_Part_Commodity_List] = 'Chassis' Then 1
   when ltrim(Rtrim(a.[PN_Combo])) like '%LCD Panel%' and b.[Dispatch_Part_Commodity_List] = 'LCD Panel' Then 1
   When ltrim(Rtrim(a.[PN_Combo])) in (' Motherboard, Memory, Documentation, Cables, Adapter, AC',' Motherboard, DVDRW Drive, Documentation',' Motherboard, Cables',' PCBA - Misc., Motherboard, Heatsink, Fan Assembly, Fan Assembly, Documentation, Cables',' Operating System, Motherboard, Heatsink, Hard Drive, Documentation',' Operating System, Motherboard, Heatsink, Hard Drive, Documentation',' Motherboard, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Documentation, Battery (Notebook)',' Operating System, Motherboard, Heatsink, Hard Drive, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Heatsink, Documentation',' Motherboard, Miscellaneous, Documentation',' Motherboard, Documentation, Cables, Battery (Notebook), Adapter, AC',' Motherboard, LCD Panel, Documentation',' Motherboard, Documentation',' Motherboard, Miscellaneous, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation, Cables',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',
' Motherboard, Memory, Documentation, Cables, Adapter, AC',' Motherboard, DVDRW Drive, Documentation',' Motherboard, Cables',' Operating System, Motherboard, Heatsink, Hard Drive, Documentation',' Operating System, Motherboard, Heatsink, Hard Drive, Documentation',' Operating System, Motherboard, Heatsink, Hard Drive, Documentation',' Motherboard, Heatsink, Documentation',' Motherboard, Miscellaneous, Documentation',' Motherboard, Documentation, Cables, Battery (Notebook), Adapter, AC',' Motherboard, Miscellaneous, Documentation',' LCD Panel, Keyboard, Chassis',' LCD Panel, Cables',' LCD, Chassis',' LCD Panel, Chassis, Chassis',' Speaker, LCD',' Speaker, Hard Drive',' Speaker, Hard Drive'
) and b.[Dispatch_Part_Commodity_List] in ('Motherboard, Memory, Documentation','Motherboard, Documentation','Motherboard','PCBA - Misc., Motherboard, Heatsink, Fan Assembly(2), Documentation, Cables','Motherboard, Documentation','Motherboard, Heatsink, Fan Assembly(2), Documentation','Motherboard(2), Documentation(2)','Motherboard, Documentation','Motherboard, Miscellaneous, Mechanical Hardware, Documentation','Motherboard, Documentation','Motherboard, Documentation','Motherboard, Documentation','Motherboard','Motherboard, Documentation, Cables','Motherboard, Miscellaneous, Mechanical Hardware, LCD Panel, Documentation','Motherboard, Fan Assembly(2), Electrical Components(4), Documentation','Motherboard, Documentation','Motherboard, Documentation','Motherboard, Heatsink, Fan Assembly, Documentation','Motherboard, Heatsink, Documentation','Motherboard, Documentation','Motherboard, Fan Assembly, Documentation','Motherboard, Heatsink, Fan Assembly, Documentation',
'Motherboard, Memory, Documentation','Motherboard, Documentation','Motherboard','Motherboard, Documentation','Motherboard, Heatsink, Fan Assembly(2), Documentation','Motherboard, Documentation','Motherboard, Documentation','Motherboard','Motherboard, Documentation, Cables','Motherboard, Documentation','LCD Panel, Chassis','LCD Panel, Chassis(2), Cables','LCD','LCD Panel, Cables','LCD','Speaker(2)','Speaker'
) then 1
Else 0 End as 'Exemption'
,Case 
   When 
  a.[PN_Combo] in (' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation, Battery (Notebook)',' Motherboard, Documentation',' PCBA - Misc., Motherboard, Documentation, Cables',' Motherboard, Documentation',' PCBA - Misc., Motherboard, Documentation, Cables',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation, Cables',' Motherboard, Documentation, Cables',' Motherboard, Documentation, Cables',' PCBA - Misc., Motherboard, Documentation, Cables',' Motherboard, Documentation',' Wireless Local Area Network, Motherboard, Memory, LCD Panel, Keyboard, Hard Drive, Documentation, Chassis, Cables, Cables, Cables',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Documentation, Battery (Notebook)',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Documentation',' Motherboard, Documentation, Cables',' Motherboard, Documentation, Cables',' Motherboard, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Documentation, Cables',' Motherboard, Documentation, Cables',' Motherboard, Documentation',' Motherboard, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation, Cables',' Motherboard, Miscellaneous, Mechanical Hardware, Documentation',' Motherboard, Documentation',' Motherboard, Heatsink, Fan Assembly, Documentation, Cables, Cables',' Motherboard, Hard Drive, Documentation',' Motherboard, Documentation',' Motherboard, Documentation, Cables',' LCD Panel, Battery (Notebook)',' Wireless Local Area Network, Motherboard, Memory, LCD Panel, Keyboard, Hard Drive, Documentation, Chassis, Cables, Cables, Cables',' Motherboard, LCD Panel, Documentation',
  ' Wireless Local Area Network, Motherboard, Documentation',' PCBA - Misc., Motherboard, Heatsink, Fan Assembly, Fan Assembly, Documentation, Cables',' Motherboard, Miscellaneous, Documentation',' Motherboard, Miscellaneous, Mechanical Hardware, Heatsink, Fan Assembly',' LCD Panel, Chassis',' LCD, Chassis',' LCD Panel, Cables',' LCD Panel, Cables'
) 
   then 1
   Else 0 End as 'Exemption 2'
,Case 
  When 
  b.[Dispatch_Part_Commodity_List] in ('Motherboard, LCD Panel','Touchpad, PCBA - Misc.(3), Motherboard, Keyboard, Documentation, Chassis, Cables(2)','Motherboard, Memory, LCD Panel, Documentation, Cables','Motherboard, LCD Panel, Documentation','Motherboard, Memory, Documentation','Touchpad, Motherboard, Documentation','Motherboard, LCD Panel, Documentation','Operating System, Motherboard, Hard Drive, Documentation, Cables','PCBA - Misc., Motherboard, Miscellaneous, Mechanical Hardware, Documentation','Wireless Local Area Network, Motherboard, Documentation, Cables','Operating System, Motherboard, Miscellaneous, Mechanical Hardware, Hard Drive(2), Documentation, Chassis, Cables(2)','Speaker, Motherboard, Documentation','Speaker, Motherboard, LCD Panel, Documentation, Chassis','Wireless Local Area Network, Motherboard, Documentation','Motherboard, LCD, Documentation','Operating System, Motherboard, Miscellaneous, Mechanical Hardware, LCD Panel, Hard Drive, Documentation, Cables','Motherboard, LCD Panel, Documentation, Cables(2)','Motherboard, LCD Panel, Documentation','Motherboard, LCD Panel, Documentation','Motherboard, Hard Drive, Documentation','Motherboard, LCD Panel, Documentation','Motherboard, LCD Panel, Documentation','Motherboard, LCD Panel, Documentation, Cables(2), Adapter, AC','Motherboard, Memory, Keyboard, Documentation','Motherboard, Miscellaneous, Mechanical Hardware, Keyboard(2), Documentation','Motherboard, Miscellaneous, Memory, Mechanical Hardware, Keyboard, Documentation','Motherboard, Miscellaneous, Mechanical Hardware, LCD Panel, Heatsink, Documentation, Cables','PCBA - Misc., Motherboard, Memory, Documentation','PCBA - Misc., Motherboard, LCD Panel, Documentation, Chassis, Cables','PCBA - Misc., Motherboard, Documentation, Chassis, Cables(2)','Motherboard, Graphics Card(2), Documentation','Motherboard, Miscellaneous, Mechanical Hardware, Graphics Card, Documentation','Motherboard, Memory, Documentation','Motherboard, Memory, LCD Panel, Documentation, Cables','Wireless Local Area Network, PCBA - Misc., Motherboard, LCD Panel, Documentation','Wireless Local Area Network, PCBA - Misc., Motherboard, LCD Panel, Documentation','Motherboard, Miscellaneous, Mechanical Hardware, Documentation, Chassis(4), Cables','Motherboard, Miscellaneous, Mechanical Hardware, LCD Panel, Documentation','Motherboard, Memory, Documentation','Motherboard, LCD Panel, Documentation, Chassis, Cables','Operating System, Motherboard, LCD Panel, Documentation','Motherboard, LCD Panel, Documentation','Motherboard, Documentation, Battery (Other)','Motherboard, LCD, Documentation','Operating System, Motherboard, Miscellaneous, Mechanical Hardware, LCD Panel, Hard Drive, Documentation, Cables','Motherboard, Miscellaneous, Mechanical Hardware, LCD Panel, Documentation',
  'Motherboard, LCD Panel, Documentation','PCBA - Misc., Motherboard, Heatsink, Fan Assembly(2), Documentation, Cables','PCBA - Misc., Motherboard, Miscellaneous, Memory, Documentation','PCBA - Misc., Motherboard, Miscellaneous, Mechanical Hardware, Documentation, Chassis','Motherboard, Mechanical Hardware(2), LCD Panel, Documentation, Chassis(3)','LCD, Keyboard, Chassis(2), Cables','Motherboard, LCD Panel, Documentation, Cables','LCD Panel, Keyboard, Chassis(3), Cables'
) 
  then 1
  Else 0 End as 'Exemption 3'

  from CTE3 a With (Nolock) inner join [GD].[o].[U_F] b With (Nolock)
  on 
  a.[ASST_ID] = b.[ASST_ID]
  LEFT OUTER JOIN [RD].[P_].[A_D] c With (Nolock)
  on
  b.[SRC_PRSN_HIST_GEN_ID]= c.[SRC_PRSN_HIST_GEN_ID]
  Left Outer Join [RD].[C_W].[F_D_C] d With (Nolock)
  on 
  b.[DSPCH_CRT_DT] = d.[ACTUAL_DATE]
  left outer join [RD].[S_P].[G_S_D_I_O_D] e With (Nolock)
  on
  b.[SVC_DSPCH_ID] = e.[SVC_DSPCH_ID]

  where a.[CONTRACT_ACTIVE] = 0
  and
 b.[FISCAL_Week] >= '201901'
 and 
 b.[DSPCH_CRT_DT] > a.[DSPCH_CRT_DT]
 and
b.[B2D_FLG] = 1
and
(a.[ASST_ID] != 'INS01' and a.[ASST_ID] != 'NONT1')

and
b.[DSPCH_STATUS_CD] != 'Cancelled'
  )
  ,CTE4 as
  (
  Select distinct *
  , case when [Dispatch_Part_Commodity_List] = ltrim(Rtrim([Original Order])) then 1 else 0 end as'Same Part Repeat'
  , case When isnull([Dispatch_Part_Commodity_List], '') = '' or isnull([Original Order], '') = '' then 1 else 0 end as 'Labor Only Repeat'
  , Case When [Dispatch_Part_Commodity_List] != ltrim(Rtrim([Original Order])) then 1 else 0 end as 'Non Original Order'
  , Case When [Dispatch_Part_Commodity_List] like Concat ('%',ltrim(Rtrim([Original Order])),'%') then 1 
    When [Exemption 2] = 1 and [Exemption 3] = 1 then 1

  else 0 end as 'Standby Flag'

  from Dispatch_Map With (Nolock)
  where Row_Count = 1
  ) 
  Select *
  ,Case
   When ([Same Part Repeat] = 1 or [Exemption] = 1) and [X_ISP_REPEAT_REAS_VAL] = 'DOA Service Part' then 'DOA Repeat'
   When ([Same Part Repeat] = 1 or [Exemption] = 1) and ([X_ISP_REPEAT_REAS_VAL] != 'DOA Service Part' or isnull([X_ISP_REPEAT_REAS_VAL], '') = '') then 'Same Part Repeat'   
   When [Labor Only Repeat] = 1 and [Same Part Repeat] = 0 and [Exemption] = 0  then 'Labor Only Repeat'
   When [Same Part Repeat] = 0 and [Labor Only Repeat] = 0 and [Standby Flag] = 0 and [Non Original Order] = 1 and [Exemption] = 0 and [Exemption 2] = 0 then 'Non Original Order'
   When ([Standby Flag] = 1 or [Exemption 2] =1) and [Non Original Order] = 1 and [Same Part Repeat] = 0 and [Labor Only Repeat] = 0 and [Exemption] = 0 then 'Standby Parts'
   Else 'Others' End as 'Part Repeat Cause'

   from CTE4 With (Nolock)

0 个答案:

没有答案