【问题标题】:How to return only the rows that above the average in a fully working query?如何在完全工作的查询中仅返回高于平均值的行?
【发布时间】:2017-08-11 20:32:52
【问题描述】:

我有以下查询,它返回一个行列表。每行都有自己的souma 列。实际返回的是 26 行。

我想要实现但不幸的是不能,只显示他们的souma这 26 行(不是整个表)中高于avg(souma) 的行。

为此我必须做出哪些改变?

SELECT * 
FROM   (SELECT a.*, 
               page, 
               Count(*) AS total 
        FROM   (SELECT *,
                       Sum(percentile_visits_frequency 
                           + percentile_time_spent + percentile_visits 
                           + percentile_last_visit 
                           + Datediff(last_visit, first_visit)) AS souma, 
                       Datediff(last_visit, first_visit)        AS 
                       diafora_imeron 
                FROM   new_table 
                WHERE  company_id = 1 
                       AND Datediff(last_visit, first_visit) > 0 
                GROUP  BY hash 
                ORDER  BY souma DESC, 
                          diafora_imeron DESC) a 
               JOIN behaviour 
                 ON ( behaviour.hash = a.hash ) 
        GROUP  BY page, 
                  hash 
        ORDER  BY total DESC) dd 
GROUP  BY hash 
ORDER  BY souma DESC

【问题讨论】:

标签: php mysql


【解决方案1】:

那么你需要一个子查询

 SELECT AVG(souma)
 FROM (SELECT souma
       FROM <yourquery>) as T  --this query is the one returning 26 rows

【讨论】:

  • 嗨,您的答案只返回一行,这是平均值。在这种情况下,平均值是 26 行的正确平均值。但是,我想输出高于此平均数的行。
  • 我看到你之前问过一个非常相似的问题。正如我所说,如果您不提供样本数据,当前输出和预期输出将成为猜谜游戏。检查这个问题,例如stackoverflow.com/questions/45511410/…
猜你喜欢
  • 1970-01-01
  • 2013-05-22
  • 2015-11-18
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多