0

我想计算警报的数量和用户处理的警报数量,键入 type = C 和 M 我从这个查询开始,它给了我正确的主要和关键警报的数量,但我被处理的警报数量卡住了

SELECT 
  TYPE,
  user_name,
  COUNT(type_alertes) AS nb, 
  id_user,
  SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
  SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
FROM stat_alert  
 LEFT OUTER JOIN user_qdf ON user_qdf.`id` = id_user 
WHERE 
  user_qdf.`id`=id_user 
  AND TYPE IN ('M', 'C')
GROUP BY id_user 

谁能帮我

4

1 回答 1

0

If you want number of all alerts and from that number of C and number of M then

SELECT 
  user_name,
  COUNT(type_alertes) AS nb, 
  id_user,
  SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
  SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
FROM stat_alert  
 JOIN user_qdf ON user_qdf.`id` = id_user 
GROUP BY id_user, user_name 

should do that. I assume that user_name is from user_qdf table and other are from stat_alert and id_user is not null.

EDIT: For sum of alertes

SELECT 
  user_name,
  COUNT(type_alertes) AS nb, 
  id_user,
  SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
  SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
  SUM(CASE WHEN TYPE ='M' OR TYPE = 'C' THEN 1 ELSE 0 END) AS both_c_and_m
FROM stat_alert  
 JOIN user_qdf ON user_qdf.`id` = id_user 
GROUP BY id_user, user_name

EDIT2:

SELECT 
  user_name,
  COUNT(type_alertes) AS nb, 
  SUM(CASE WHEN TYPE ='C' THEN type_alertes ELSE 0 END) AS critique_alerts,
  SUM(CASE WHEN TYPE ='M' THEN type_alertes ELSE 0 END) AS majeur_alertes
  id_user,
  SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
  SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
FROM stat_alert  
 JOIN user_qdf ON user_qdf.`id` = id_user 
WHERE TYPE in ('C','M')
GROUP BY id_user, user_name

is that what you want?

于 2012-11-12T12:00:11.520 回答