需要根据条件

时间:2017-09-01 11:55:51

标签: mysql sql

在下面提到的查询之一中需要帮助,我想要计算id(Distinct)的条件, 1)如果与该id相关的所有三个结构都具有STATUS AS" SUCCESS"然后它被称为交付。 2)如果任何结构只有1或2个结构相关,并且状态是成功但结构中的一个缺失则其待定。
3)如果所有三个结构都存在但结构状态中的任何一个失败,那么它将被计入未决状态。

这是我写的查询以获得下表,

   select m.id, p.group, p.structure, p.STATUS 
    from table_M m
    join table_O o on o.id = m.id
    join table_P p on ofenIdent.ref_id = p.ref_id
    where m.group = 'grp01'
      and p.status_flag = 'process' 
      and p.structure IN ('X','Y','Z')
    limit 50;


| id | group | structure    | STATUS  |
|------------|-----|-------------------|---------|
|        7 | grp01 | X          | SUCCESS |
|        7 | grp01 | X          | SUCCESS |
|        7 | grp01 |Y           | SUCCESS |
|        7 | grp01 |Y           | SUCCESS |
|        7 | grp01 | Z          | SUCCESS |
|        7 | grp01 | Z          | SUCCESS |
|        9 | grp01 | X          | SUCCESS |
|        9 | grp01 |Y           | SUCCESS |
|        9 | grp01 | Z          | FAILURE |
|        0 | grp01 | X          | SUCCESS |
|        0 | grp01 |Y           | SUCCESS |
|        0 | grp01 | Z          | FAILURE |
|        2 | grp01 | X          | SUCCESS |
|        2 | grp01 |Y           | SUCCESS |
|        2 | grp01 | Z          | FAILURE |
|        4 | grp01 | X          | SUCCESS |
|        4 | grp01 | X          | SUCCESS |
|        4 | grp01 | X          | SUCCESS |
|        4 | grp01 | X          | SUCCESS |
|        4 | grp01 | X          | SUCCESS |
|        3 | grp01 | X          | SUCCESS |
|        3 | grp01 |Y           | SUCCESS |
|        3 | grp01 | Z          | FAILURE |
|        4 | grp01 |Y           | FAILURE |
|        4 | grp01 |Y           | FAILURE |
|        4 | grp01 |Y           | FAILURE |
|        4 | grp01 |Y           | FAILURE |
|        4 | grp01 |Y           | FAILURE |
|        4 | grp01 | Z         | SUCCESS |
|        4| grp01 | Z         | SUCCESS |
|        4 | grp01 | Z         | SUCCESS |
|        4 | grp01 | Z         | SUCCESS |
|        4 | grp01 | Z         | SUCCESS |
|        4 | grp01 | Z       | SUCCESS |
|        4 | grp01 | Z       | SUCCESS |
|        4 | grp01 | Z       | SUCCESS |
|        4 | grp01 | Z       | SUCCESS |
|        4 | grp01 | Z       | SUCCESS |
|        5 | grp01 | X        | SUCCESS |
|        5 | grp01 | X        | SUCCESS |
|        5 | grp01 | X        | SUCCESS |
|        5 | grp01 | X         | SUCCESS |
|        6 | grp01 | X         | SUCCESS |
|        5 | grp01 |Y          | SUCCESS |
|        5 | grp01 |Y          | SUCCESS |
|        5 | grp01 |Y          | SUCCESS |
|        5 | grp01 |Y          | SUCCESS |
|        5 | grp01 | Z         | SUCCESS |
|        5 | grp01 | Z         | SUCCESS |
|        5 | grp01 | Z         | SUCCESS |

这是MYSQL db。

预期结果应该是,

Structure status |Total Count
-----------------|-----------
Delivered        | 2

Structure status |Total Count
-----------------|-----------
Pending          | 6

根据要求提供更多细节,我无法提及太多数据,但这是我能提供的。

表table_M:

M_id| id| group 
----|---|--------
1   |7  |grp01 
2   |9  |grp01
3   |0  |grp01
4   |2  |grp01
5   |4  |grp01
6   |5  |grp01
7   |6  |grp01
8   |3  |grp01

表table_O:

O_id| id| ref_id 
----|---|-------
1   |0  |xxx01 
2   |1  |xxx02
3   |2  |xxx03
4   |3  |xxx04
5   |4  |xxx05
6   |5  |xxx06
7   |6  |xxx07
8   |7  |xxx08

表table_P:

|P_id   | ref_id | structure| STATUS|group|status_code|
|--------|--------|----------|-------|-----|------------|
    1   |xxx01   |  X       |SUCCESS|grp01| 2|
    2   |xxx01   |  Y       |SUCCESS|grp01| 2|
    3   |xxx01   |  Z       |SUCCESS|grp01| 2|
    4   |xxx02   |  X       |SUCCESS|grp01| 2|
    5   |xxx03   |  X       |SUCCESS|grp01| 2|
    6   |xxx03   |  Y       |FAILURE|grp01| 2|
    7   |xxx03   |  Z       |FAILURE|grp01| 2|
    8   |xxx04   |  X       |SUCCESS|grp01| 2|
    8   |xxx04   |  Y       |SUCCESS|grp01| 2|
    8   |xxx04   |  Z       |SUCCESS|grp01| 2|
    8   |xxx06   |  X       |FAILURE|grp02| 2|
    8   |xxx06   |  Y       |FAILURE|grp02| 2|
    8   |xxx06   |  Z       |FAILURE|grp02| 2|

2 个答案:

答案 0 :(得分:0)

这应该让你开始。它不是一个完整的答案,但是评论太长了。

select somefields to be discussed later
, count(distinct id) "Total Records"

from yourTable left join (
select id xid
, status xStatus
from yourTable
where structure = 'X' ) x on xid = yourTableId
)
-- repeat for Y and Z
group by somefields to be discussed later

您真正想要的字段将是满足您要求的案例构造。一个例子是:

case when xStatus = 'SUCCESS' and yStatus = 'SUCCESS' and zStatus = 'SUCCESS'
then 'Delivered'
when something else then an appropriate value
else default value end "Stucture Status"

答案 1 :(得分:0)

SELECT structure_status, COUNT(DISTINCT id)
FROM (SELECT id,
        CASE
          -- Case 1
          WHEN success_count = 3 THEN 'Success'
          -- Case 2
          WHEN success_count > 0 AND failure_count = 0 THEN 'Pending'
          -- Case 3 - also called 'Pending'?
          WHEN total_count = 3 AND failure_count > 0 THEN 'Pending'
          -- Case 4 - what to do here?
          ELSE 'XXX'
        END structure_status
      FROM (SELECT m.id,
              count(*) total_count,
              count(CASE status WHEN 'SUCCESS' THEN 1 END) success_count,
              count(CASE status WHEN 'FAILURE' THEN 1 END) failure_count
            FROM table_M m
            JOIN table_O o ON o.id = m.id
            JOIN table_P p ON ofenIdent.ref_id = p.ref_id
            WHERE m.group = 'grp01'
              AND p.status_flag = 'process' 
              AND p.structure IN ('X','Y','Z')
            GROUP BY m.id) status_count
     ) structure_status_mapping
GROUP BY structure_status