MySql使用相同的查询随机检索不同的值?

时间:2014-03-18 05:03:32

标签: mysql sql stored-procedures syntax left-join

此查询有时会在某些行中随机地给出空值,有时它不会。当我改变左连接的内部连接时,这一切都开始发生了。

CREATE temporary TABLE IF NOT EXISTS surveys_temp AS 
  (SELECT SN.id_rep, 
          Coalesce(( Sum(CASE 
                           WHEN SN.score < 7 THEN -100 
                           WHEN SN.score >= 7 
                                AND score < 9 THEN 0 
                           WHEN SN.score >= 9 THEN 100 
                         end) / Count(score) ), 0) AS NRS, 
          SW.wtr 
   FROM   surveys SN 
          INNER JOIN (SELECT id_rep, 
                             Coalesce(( Sum(CASE 
                                              WHEN score < 7 THEN -100 
                                              WHEN score >= 7 
                                                   AND score < 9 THEN 0 
                                              WHEN score >= 9 THEN 100 
                                            end) / Count(score) ), 0) AS WTR 
                      FROM   surveys 
                      WHERE  survey_type = 'WTR' 
                      GROUP  BY id_rep) SW 
                  ON SW.id_rep = SN.id_rep 
   WHERE  SN.survey_type = 'NRS' 
   GROUP  BY SN.id_rep); 

CREATE temporary TABLE IF NOT EXISTS orders_temp AS 
  (SELECT id_rep, 
          Sum(Cast(ordernumber AS DECIMAL(2, 0))) AS Orders 
   FROM   orders 
   GROUP  BY id_rep); 

CREATE temporary TABLE IF NOT EXISTS chats_temp AS 
  (SELECT id_rep, 
          Time_format(Sec_to_time(Cast(Cast(Sum(response_time * -1)/ Count( 
                                  id_session) AS 
                                  DECIMAL(5, 2 
                                              )) AS CHAR(6 
                                  ))), '%H : %i : %s')AS response_time 
   FROM   chats 
   WHERE  chat_type = 1 
   GROUP  BY id_rep 
   ORDER  BY id_rep); 

SELECT R.rep_name, 
       Count(DISTINCT R.id_session)                AS Chats, 
       O.orders, 
       Concat(Cast((o.orders/Count(DISTINCT r.id_session)) * 100 AS DECIMAL(5, 2 
              )), '%' 
       )                                           AS CONVERSION, 
       Coalesce(Cast(s.nrs AS DECIMAL(5, 2)), '0') AS NRS, 
       Coalesce(Cast(s.wtr AS DECIMAL(5, 2)), '0') AS WTR, 
       C.response_time 
FROM   reps R 
       LEFT JOIN surveys_temp AS S 
              ON S.id_rep = R.id_rep 
       LEFT JOIN orders_temp AS O 
              ON O.id_rep = R.id_rep 
       LEFT JOIN chats_temp AS C 
              ON c.id_rep = R.id_rep 
WHERE  R.rep_country IN( 'D.R', 'U.S' ) 
GROUP  BY R.rep_name 
ORDER  BY R.rep_name; 

1 个答案:

答案 0 :(得分:3)

这里的一个问题是,您违反了Single Value Rule,因为您只是通过R.Rep_Name进行分组,而是选择其他字段而不通过汇总功能引入它们,例如列O.OrdersC.response_time(以及通过CoalesceConcat派生的列)

由于此违规行为,很可能并非所有未分组的非聚合列的值都与每个Rep_Name组具有相同的值,并且结果是不确定的。 More here

修改(来自以下评论)

  

数据看起来好一点,但有些rep_names正在重复

这似乎确认非聚合列不是唯一的。所以:

  • 这些列无法在rep_name组的上下文中显示,
  • 或者你有比你想象的更多的离散群体
  • 或者您需要使用Count(O.Orders), AVG(C.response_time)等聚合来限定非不同列,然后更改列标题(例如&#34; TotalOrders&#34;,&#34; AverageResponseTime&# 34;等)