Написание одного запроса для Mutliple, где условие, используя тот же столбец той же таблицы - PullRequest
0 голосов
/ 22 октября 2011

В моей таблице одним из полей является msg. На основании этого поданы два условия. Состояние

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

здесь имя столбца a, b, c

Я хочу написать вышеуказанное требование одним запросом. Если я пишу два запроса на основе двух, где условие, то я получаю результат, но я хочу написать один запрос и показать свой результат следующим образом:

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

Ответы [ 2 ]

1 голос
/ 22 октября 2011
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 голосов
/ 23 октября 2011

Вы можете использовать оператор UNION для объединения двух запросов с одинаковым количеством столбцов:

Я не уверен, что вы пытаетесь сделать, но оператор UNION из таблицы, которую вы упомянули, будет выглядеть примерно так:

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