SQL:查找具有相关字段的最大平均值

时间:2014-02-18 17:29:45

标签: mysql sql

我在使用相关字段max)获取provider_state平均值时遇到问题。

我有一张桌子。其中有average_total_payments字段用于付款。每行还有状态字段provider_state。从下面的查询中我得到Max Avg,但我没有得到相关的provider_state

此查询的主要目的是为我提供具有最高付款平均值的州名称,包括付款金额。

的mysql_query

SELECT provider_state
       ,MAX(average) AS max_average
    FROM ( SELECT provider_state
               ,AVG(average_total_payments) AS average
            FROM gnomics
            WHERE drg_definition LIKE '%$search%'
            GROUP BY provider_state ) AS abc

请给我一些建议,了解相关领域的最佳途径是什么?

2 个答案:

答案 0 :(得分:0)

如果您的问题是“请说明具有最高付款平均金额的州的名称,包括付款金额”。然后你可以这样解决:

SELECT provider_state, AVG(average_total_payments) AS average
FROM gnomics
WHERE drg_definition LIKE '%$search%'
GROUP BY provider_state
ORDER BY average desc
LIMIT 1;

答案 1 :(得分:0)

如果我理解了这个问题:

SELECT provider_state, field1, field2 ....
      ,MAX(average) AS max_average
   FROM ( SELECT provider_state, field1, field2 ....

           ,AVG(average_total_payments) AS average
        FROM gnomics
        WHERE drg_definition LIKE '%$search%'
        GROUP BY provider_state, field1, field2 ....
) AS abc

应该做的工作