简体   繁体   English

SQL-返回具有MAX COUNT值的行

[英]SQL - return rows for values with MAX COUNT

I want to access a weather table and sumamrise it in terms of days and months. 我想访问一个天气表并将其按天和月求和。 I want some of the values to be AVG and some to be SUM. 我希望某些值是AVG,另一些值是SUM。

I want to underpin the resulting record with values from the collective data that represent the maximum count but after a few combinations, I have not managed it. 我想用代表最大计数的集体数据中的值作为结果记录的基础,但是经过几次组合后,我还没有对其进行管理。

EXAMPLE DATA: 示例数据:

day_date                main_weather     temp
2012-01-01 07:00:00     Cloudy           8.0
2012-01-01 08:00:00     Cloudy           10.0
2012-01-01 09:00:00     Sunny            12.0
2012-01-01 10:00:00     Sunny            16.0
2012-01-01 11:00:00     Sunny            18.0

WANTED RESULT: 想要的结果:

DATE(day_date)          MAX(COUNT(main_weather)     AVG(temp)
2012-01-01              Sunny                       12.8

Here's my first SQL to show what I am trying to do: 这是我的第一个SQL,显示我要执行的操作:

SELECT 
    DATE(`day_date`), 
    MAX(COUNT(`main_weather`)),    <--- this is the piece I am stuck with the max values.
    AVG(`temp`) 
FROM `sma_weather`
GROUP BY `day_date`;

For your second example, the query return only one row because you have added the option LIMIT 1 . 对于第二个示例,查询仅返回一行,因为您已添加了LIMIT 1选项。 FOr the rest, I don't really understand your description of the result that you want to get so I cannot help you. 其余的,我不太了解您对要获得的结果的描述,所以我无能为力。 It wouldn't hurt to give a litle example with a few lines of data so that people can understand what you are trying to achieve. 举一个带有几行数据的小例子,这样人们就可以理解您要实现的目标,这没有什么坏处。

Looks like a correlated subquery would do it, but not sure how well this will scale. 看起来相关的子查询可以做到,但是不能确定它的扩展程度。

SELECT 
      DATE(`day_date`)    AS day
    , (
        SELECT  w1.`main_weather`
        FROM `weather` w1
        where DATE(w1.`day_date`) = DATE(`weather`.`day_date`) 
        GROUP BY
              DATE(`day_date`)
            , `main_weather`
        order by count(*) DESC
        limit 1
      )                   AS max_count_weather
    , AVG(`temp`)         AS av_temp
FROM `weather`
GROUP BY
      DATE(`day_date`)
;

See this SQLFiddle 看到这个SQLFiddle

Try something like (untested): 尝试类似(未试用)的方法:

select day_date, (select top 1 main_weather from MyTable t2 where date(t1.day_date) = date(t2.day_date) group by main_weather
order by count(*) desc) as Max_MainWeather, avg (temp) as AvgTemp
from MyTable t1
group by (day_date)

As far as I understand your problem (as it was not easy for me to understand), I came up with the following solution: 据我了解您的问题(因为我不容易理解),我提出了以下解决方案:
select t.on_date, t.main_weather, avg(temp) from 从中选择t.on_date,t.main_weather,avg(temp)
(select date(day_time) as on_date, main_weather, avg(temp) as avgtemp (选择date(day_time)作为on_date,main_weather,avg(temp)作为avgtemp
from weather group by on_date,main_weather order by main_weather desc limit 1 来自天气组(按on_date,main_weather顺序,按main_weather desc限制1)
) as t join weather on date(day_time)=t.on_date group by t.on_date; )作为t加入天气on date(day_time)= t.on_date group by t.on_date;

The query is tested and works fine for me. 该查询已经过测试,对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM