如何根据工作流程状态进行分类?

时间:2016-01-04 03:52:38

标签: sql

我有一个工作流程表,我想根据工作流程状态将它们分为两类。示例如下。我有3个唯一ID,每个唯一ID具有不同的工作流程状态。如果唯一ID的工作流包含“缺少支持文档”,“返回所有者进行更正”,我想将整个唯一ID分类为“错误的应用程序”。否则,将唯一ID分类为“良好应用程序”

UniqueID   Status
==================
ABC123      Application Submitted
ABC123      Work in Progress
ABC123      Application Approved
ABC123      Application Activated
DEF567      Submission
DEF567      Work in Progress
DEF567      Missing Support Documentation
DEF567      Return to Owner for Correction
DEF567      Resubmitted
DEF567      Work in Progress
KFG678      Application Submitted
KFG678      Work in Progress
KFG678      Application Approved
KFG678      Application Activated

我想结果是:

UniqueID   Status                            Application_Category
=================================================================
ABC123      Application Submitted            Good application
ABC123      Work in Progress                 Good application
ABC123      Application Approved             Good application
ABC123      Application Activated            Good application
DEF567      Submission                       Bad application
DEF567      Work in Progress                 Bad application
DEF567      Missing Support Documentation    Bad application
DEF567      Return to Owner for Correction   Bad application
DEF567      Resubmitted                      Bad application
DEF567      Work in Progress                 Bad application
KFG678      Application Submitted            Good application
KFG678      Work in Progress                 Good application
KFG678      Application Approved             Good application
KFG678      Application Activated            Good application

3 个答案:

答案 0 :(得分:0)

只要用好就可以了

    select 
      UniqueID, status,
      case when status in ('Missing Support Documentation',
          'Return to Owner for Correction') then 'Bad application'
           else 'Good application' end as Application_Category
    from table

您可以在查询中使用inert into来创建一个新表来存储数据。

好吧,这是我的错,我没有明确得到你的要求,现在我做了这个并在Mysql和MS SQL上测试,我认为它适用于Oracle

    select UniqueID,status,'Bad application' as Application_Category from table a 
      where exists
       (select 1 from
            (select distinct UniqueID
               from table where status in ('Missing Support Documentation',
      'Return to Owner for Correction')) b 
                 where a.UniqueID = b.UniqueID)

    UNION ALL

    select UniqueID,status,'Good application' as Application_Category from table a 
      where not exists
         (select 1 from
            (select distinct UniqueID from table 
               where status in ('Missing Support Documentation',
      'Return to Owner for Correction')) b 
                 where a.UniqueID = b.UniqueID) 

或者您可以使用像这样的临时表

      select distinct UniqueID into table1 from table 
               where status in ('Missing Support Documentation',
      'Return to Owner for Correction');

       select a.UniqueID,a.status,
              case when b.UniqueID is null then "Good application" 
                   else "Bad application" end as Application_Category
       from table a left join table1 b on a.UniqueID=b.UniqueID

答案 1 :(得分:0)

使用IF关键字即可。然后使用IN关键字查找列是否包含多个值的数组。

然后,您需要使用AS关键字,因为之前的列名称长得令人难以置信。我们可以使用该关键字重命名它。

以下是mysql的内容。

SELECT UNIQUE_ID, STATUS, 
IF(STATUS IN ("Missing Support Documentation", "Return to owner for correction"), 
"Bad Application", "Good Application") AS application_grade
FROM table

我在这里创建了sqlfiddle

http://sqlfiddle.com/#!9/eff5b/6/0

答案 2 :(得分:0)

你可以使用像{/ p>这样的CASE表达式来实现

select t1.UniqueID, t1.Status, 
case when t1.UniqueID = t2.UniqueID
then 'Bad Application' else 'Good Application' end as Application_Category
from mytable t1
left join ( select distinct UniqueID from mytable 
where Status in ( 'Missing Support Documentation', 'Return to Owner for Correction')) t2
on t1.UniqueID = t2.UniqueID
order by t1.UniqueID;

source