在3个表中选择count(*)和“join”

时间:2011-03-01 08:56:54

标签: sql select join count

我在3个表的连接中寻找一个计数查询,可以让我对其中一个表的不同值进行计数。

我需要加入3个表来获取预期数据(WorkflowMessageMessage_Workflow_Relation)。

我想获得按结果(Message)中状态+连接的related_name表的一个字段分组的工作流计数。相关名称应取自adapter字段等于wf的条目,但有时会有多条Message记录符合此条件,这将导致我的计数中的数据集更多,然后是真的在那里。

我很确定必须能够解决这个问题,但不要让它发挥作用。 遗憾的是,我无法更改表结构,因为它是我们使用的产品的给定模式。

我的表格结构如下:

工作流:

id | workflow_id | starttime | endtime | status
------------------------------------------------------
1  |          22 |         0 |     200 |     OK
2  |          23 |       220 |     920 |  ERROR
3  |          55 |       202 |     588 |     OK

Message_Workflow_Relation:

id | message_id | workflow_id |
-------------------------------
1  |        122 |          22 |
2  |        235 |          22 |
3  |        456 |          22 |
4  |        982 |          22 |
5  |        444 |          23 |
6  |        445 |          23 |
7  |        585 |          55 |
8  |        738 |          55 |
9  |        399 |          55 |

邮件:

id | message_id | starttime | endtime | adapter | related_name |
----------------------------------------------------------------
 1 |        122 |         0 |    2335 |      wf |   workflow_1 |
 2 |        235 |       222 |    1000 |   other |        other |
 3 |        456 |       343 |    2330 | another |      another |
 4 |        982 |       222 |    2200 |      wf |   workflow_1 |
 5 |        444 |      2223 |    3333 |      wf |   workflow_2 |
 6 |        445 |      1123 |    1244 |  manual |       manual |
 7 |        585 |      5555 |    5566 |      wf |   workflow_1 |
 8 |        738 |       655 |     999 |      wf |   worfklow_1 |
 9 |        399 |      6655 |    7732 | another |      another |

这应该返回以下结果:

count(*) | related_name | status |
----------------------------------
       2 |   workflow_1 |     OK |
       1 |   workflow_2 |  ERROR |

我对以下声明感到困惑,但我不知道该怎么做 对每个工作流程adapter = wf unique进行选择:

select distinct
  count(*),
  m.related_name,
  w.status
from
  workflow as w,
  message as m,
  msg_bpm_rel as rel
where rel.workflow_id = w.workflow_id
  and rel.message_id = m.message_id
  and m.adapter = 'PE'
group by m.related_name,w.status

这会返回给我(4 workflow_1而不是2):

count(*) | related_name | status |
----------------------------------
       4 |   workflow_1 |     OK |
       1 |   workflow_2 |  ERROR |

如何才能做出正确的查询来实现这一目标?

任何帮助表示感谢。

3 个答案:

答案 0 :(得分:4)

您可以通过对不同的值进行分组和计数来完成此操作。

类似于:

select count(distinct w.workflow_id), m.related_name,w.status 
from workflow as w, message as m, msg_bpm_rel as rel 
where rel.workflow_id = w.workflow_id and rel.message_id = m.message_id 
and m.adapter = 'PE' 
group by m.related_name, w.status

这是未经测试但我应该相信:)

答案 1 :(得分:0)

我首次尝试让查询正常工作。我不喜欢使用不同的。这让我觉得可能还有一些问题:

SELECT distinct theCount
      ,m.related_name
      ,w.status
FROM workflow as w
    ,message as m
    ,msg_bpm_rel as rel
    ,(SELECT count(1) as theCount
            ,w.workflow_id as wf_id
      FROM workflow as w
          ,message as m
          ,msg_bpm_rel as rel 
      WHERE rel.workflow_id = w.workflow_id 
      AND   rel.message_id = m.message_id 
      AND   m.adapter = 'wf' 
      GROUP BY w.workflow_id) AS t

WHERE t.wf_id = w.workflow_id 
AND   rel.workflow_id = w.workflow_id 
AND   rel.message_id = m.message_id 
AND   m.adapter = 'wf'

重要的是要注意在此查询中如何执行计数。您只是在两列上进行分组,这两列恰好位于SQL语句的SELECT部分​​中。实际情况是,要计算您想要的数量,您必须仅按工作流ID进行分组。此查询执行此操作,然后将该结果提供给另一个查询以显示您想要的内容。

答案 2 :(得分:0)

    SELECT distinct theCount
    ,m.related_name
    ,w.status
    FROM workflow as w
    ,message as m
    ,msg_bpm_rel as rel
    ,(SELECT count(1) as theCount
    ,w.workflow_id as wf_id
    FROM workflow as w
    ,message as m
    ,msg_bpm_rel as rel 
    WHERE rel.workflow_id = w.workflow_id 
    AND   rel.message_id = m.message_id 
    AND   m.adapter = 'wf' 
    GROUP BY w.workflow_id) AS t
    WHERE t.wf_id = w.workflow_id 
    AND   rel.workflow_id = w.workflow_id 
    AND   rel.message_id = m.message_id 
    AND   m.adapter = 'wf'