如何至少过滤一次条件为真的地方

时间:2019-07-08 20:47:59

标签: sql

我需要过滤到仅在其至少一项任务中具有“服务”工作组值的服务订单。但是,如果至少一个任务行具有该值,我就不想摆脱不是work group =“ Service”的行。最终结果将排除服务订单中所有不包含至少一个等于“ SERVICE”的BI_WRKFLW_TASK_KEY的数据。我知道如何做普通的过滤器,但要达到这种特殊性超出了我目前的经验。

我已经尝试过使用普通过滤器,但是它们忽略了属于同一服务订单的行,但没有该工作组。

SELECT W.BI_WRKFLW_KEY, 
       T.BI_WORK_EVENT_CD, 
       T.BI_TASK_CD, 
       T.BI_WORKGRP, 
       **M.BI_SO_NBR**, 
       M.BI_SO_TYPE_CD, 
       M.BI_CLOSE_DT,
       M.BI_OPEN_DT, 
       M.BI_SO_STAT_CD, 
       R.BI_WRKFLW_TMPLT_NM, 
       T.BI_WRKFLW_TASK_SEQ_NBR, 
       T.BI_WORKGRP, 
       A.BI_WORK_EVENT_CD,
       A.BI_EVENT_DT_TM, 
       A.SY_JOB_QUEUE_ID, 
       **A.BI_WORKGRP**, 
       A.SY_USER_ID, 
       **A.BI_WRKFLW_TASK_KEY**
FROM BI_WRKFLW W
         LEFT JOIN BI_WRKFLW_TASKS T ON W.BI_WRKFLW_KEY = T.BI_WRKFLW_KEY
         LEFT JOIN BI_SO_DET D ON W.BI_WRKFLW_KEY = D.BI_WRKFLW_KEY
         LEFT JOIN BI_SO_MASTER M ON D.BI_SO_NBR = M.BI_SO_NBR
         LEFT JOIN BI_WRKFLW_TMPLT_REF R ON W.BI_WRKFLW_TMPLT_ID = R.BI_WRKFLW_TMPLT_ID
         LEFT JOIN BI_TASK_ACT A ON T.BI_WRKFLW_TASKS_KEY = A.BI_WRKFLW_TASKS_KEY
WHERE M.BI_OPEN_DT >= ADD_MONTHS(CURRENT_DATE, -'12')
--AND M.BI_SO_TYPE_CD IN ('IVC-NEW1') 
--AND M.BI_SO_STAT_CD LIKE 'O'  
ORDER BY M.BI_SO_NBR, T.BI_EVENT_DT_TM 

任何保留有至少一个BI_WRKFLOW_TASK_CD =“ Service”的服务订单行都将保留,并过滤掉所有其他服务订单。

2 个答案:

答案 0 :(得分:0)

我试图将其映射出来,我可能不太正确,

我认为您要查询的BI_SO_MASTER记录中有> = 1个BI_WRKFLW_TASKS属于某个组。

尝试使用CTE获取具有正确任务的明细行,然后您可以找到SO人口...然后您可以不确定最终结果集的目标是什么?

;with matchingTasks as (    D.BI_SO_NBR, D.<id> , W.BI_WRKFLW_KEY , T.<key> , A.Key
from BI_WRKFLW W
LEFT JOIN BI_WRKFLW_TASKS T ON W.BI_WRKFLW_KEY = T.BI_WRKFLW_KEY
LEFT JOIN BI_SO_DET D ON W.BI_WRKFLW_KEY = D.BI_WRKFLW_KEY
LEFT JOIN BI_TASK_ACT A ON T.BI_WRKFLW_TASKS_KEY = A.BI_WRKFLW_TASKS_KEYW
Where 
    <good dates> 
    and <A.field is what I am looking for>
)
/*Here you have the SO population
as well as the ids that helped this SO qualify. 
*/
, My_SO_Population as (select Distinct BI_SO_NBR from matchingTasks   )

/*now you can go get what you need. 
the challenge of finding SOs w/ >=1 matching task has been solved...
*/
   select  <necessary fields> from    
My_SO_Population 
join   <whatever you need....this is where i am cloudy>

如果我没有实现目标,请告诉我...

答案 1 :(得分:0)

您可以将其添加到您的WHERE子句中:

AND T.BI_WRKFLW_KEY IN (
    SELECT BI_WRKFLW_KEY
    FROM BI_WRKFLW_TASKS
    WHERE BI_WRKFLOW_TASK_CD = 'Service')