2011-03-01 58 views
4

我正在查找3個表格的連接中的計數查詢,這些查詢可以使我計算其中一個表格的不同值。在3個表格中選擇count(*)和「join」

我有3個表,我需要加入以獲得預期的數據(Workflow,MessageMessage_Workflow_Relation)。

我想在結果(related_name)中獲取按狀態+連接的Message表的一個字段分組的工作流的數量。相關名稱應取自adapter字段等於wf的條目,但有時會有多條消息記錄與此條件相匹配,這將導致我的計數中出現更多數據集,然後出現真實存在的數據集。

我很確定它必須是可能的,但是不要讓它工作。 很遺憾,我無法更改表結構,因爲它是我們使用的產品的給定模式。

我的表結構是這樣的:

工作流程:

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 

This returns me(4 workflow_1而不是2):

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

如何才能做出正確的查詢來實現這一目標?

任何幫助表示讚賞。

+0

我已經給你一個答案,但一次又一次地讀你的帖子,我感覺我錯過了一些信息。我發現工作流程_1沒有明顯的價值,只有兩次,它是四次或一次。所以要麼我缺少一張桌子。你試圖指望什麼專欄? (一般來說count(*)是個不錯的主意,特別是在加入時) – 2011-03-01 09:10:51

回答

3

您可以通過對不同的值進行分組和計數來實現。

因此,像:

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 

這是未經測試,但應該工作,我相信:)

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進行分組。該查詢執行該操作,然後將該結果提供給另一個查詢以顯示您想要的內容。