如何正确地将一个选择注入其他选择

时间:2016-03-04 13:39:07

标签: mysql sql

我有一个工作查询,它​​返回平均值

SELECT r.client_id, r.mcc, r.mnc, r.rate, (max(r.rate) + min(r.rate)) / 2 As average 
FROM messages m
INNER JOIN rates r 
on m.rate_id = r.id
GROUP BY r.client_id  

效果很好

但我需要得到"平均"在这里:

...FROM (
      SELECT
        m.r_account_id,
        c.mcc,
        c.mnc,
        SUM(1)        AS total
        -- Average // i need to get value in here!!!
      FROM
        messages AS m

      JOIN
        costs AS c ON (
          c.id = m.cost_id
        )



JOIN
rates AS r ON (
r.id = m.rate_id
)



      WHERE
        m.create_timestamp BETWEEN FROM_UNIXTIME(:date_from/1000) AND FROM_UNIXTIME(:date_to/1000)
      GROUP BY
        r_account_id,
        mcc,
        mnc
    ) AS s1

这个群体应该是:  通过...分组         client_account_id,         MCC,         MNC

但是当我这样输入时: 通过...分组   client_account_id, 我可以计算出平均值 enter image description here

请帮助

0 个答案:

没有答案