2011-10-22 48 views
-1

在我的表中一個提交的是味精。基於這兩個條件提出。條件是寫一個多重查詢條件使用相同的列相同的表

where msg like '%fatal%' or msg like '%exception%' or msg like '%fopen%' 

then Select telco , 
Sum(Case when a= '1' then 1 else 0 end) as a, 
Sum(Case when b= '2' then 1 else 0 end) as b, 
Sum(Case when c= '3' then 1 else 0 end) as c, 


where msg not like '%fatal%' or msg not like '%exception%' or msg not like '%fopen%' 
then Select telco , 
Sum(Case when a= '1' then 1 else 0 end) as a_e, 
Sum(Case when b= '2' then 1 else 0 end) as b_e, 
Sum(Case when c= '3' then 1 else 0 end) as c_e, 

From temp_inbox group by t 

這裏,B,C列名

我想寫上述要求一個查詢。如果我寫了兩個查詢基兩大WHERE條件,然後我得到的結果,但我想編寫一個查詢,並顯示在下面的方式我的結果:

a b c a_e b_e c_e 

5 6 7 10 4  10 
1 2 7  45 20 2 

樣本數據:

  a b c msg 

     1 0 0 fatalerror 

     0 0 3 successed 
     1 0 0 exception 
     0 2 0 successful 
+0

可以請你發佈的內容是兩個查詢?您提供的查詢示例超出了任何sql標準。我真的不明白你在這裏做什麼。 –

+0

請閱讀SQL總結和案例教程,然後你會明白我說什麼 – salma

+0

你可以請發佈一個鏈接到教程... 並不是這個相同的問題:http://stackoverflow.com/questions/7857954 /寫一個單一查詢爲多重查詢與同樣在哪裏條件同一表 –

回答

1
select telco, 
sum(
    case when (msg like '%fatal%' or out_msg like '%exception%' or out_msg like '%fopen%') and a = '1' 
    then 1 else 0 end 
) as a, 
sum(
    case when (msg like '%fatal%' or out_msg like '%exception%' or out_msg like '%fopen%') and a = '2' 
    then 1 else 0 end 
) as b, 
sum(
    case when (msg like '%fatal%' or out_msg like '%exception%' or out_msg like '%fopen%') and a = '3' 
    then 1 else 0 end 
) as c, 
sum(
    case when (msg not like '%fatal%' or out_msg not like '%exception%' or out_msg not like '%fopen%') and a = '1' 
    then 1 else 0 end 
) as a_e, 
sum(
    case when (msg not like '%fatal%' or out_msg not like '%exception%' or out_msg not like '%fopen%') and a = '2' 
    then 1 else 0 end 
) as b_e, 
sum(
    case when (msg not like '%fatal%' or out_msg not like '%exception%' or out_msg not like '%fopen%') and a = '3' 
    then 1 else 0 end 
) as c_e 

From temp_inbox group by t 

或您可以在嵌入時使用嵌套的情況

+0

沒有先生,我想總結a,b,c列基於msg列條件 – salma

+0

@salma你能請將表格中的示例數據發佈到您運行此查詢的對象中,因爲我無法理解您的查詢。 –

+0

已經給出了樣本數據 – salma

0

可以使用UNION語句來加入兩列具有相同列數的查詢:

我不知道你有什麼做的,但你所提到會是這個樣子的表UNION語句:

select sum(a), message from telco where message like '%fatal%' 
UNION 
select sum(a), message from telco where message not like '%fatal%' 
+0

我想在兩列中分別輸出結果,一個用於'%fatal%'之類的消息,另一個用於不像'%fatal%'的消息。但你查詢給一個結果總和「消息,如'%fatal%'和消息不像'%致命%'' – salma

相關問題