我正在寻找一个函数来从表中返回最主要的非数字值。
我的数据库表记录了一个气象站的读数。其中许多是数字,但风向被记录为16个文本值之一- N,NNE,NE,ENE,E.等在一个伐查尔领域。记录每15分钟加一次,因此95行代表一天的天气。
我试着计算一天的主要风向。手动将Ns、NNEs、NEs等的数字加在一起,看哪一个是最多的。
MySQL有一个很好的方法来做这个吗?
谢谢
发布于 2014-01-03 15:19:09
很难在没有看到您的模式的情况下回答您的问题,但这将对您有所帮助。
假设风向与要忽略的数值存储在同一列中,则可以使用REGEXP忽略数值,如下所示:
select generic_string, count(*)
from your_table
where day = '2014-01-01'
and generic_string not regexp '^[0-9]*$'
group by generic_string
order by count(*) desc
limit 1如果风向是唯一存储在列中的东西,那么它就更简单了:
select wind_direction, count(*)
from your_table
where day = '2014-01-01'
group by wind_direction
order by count(*) desc
limit 1您可以使用子查询完成这一任务好几天。例如(假设将来没有任何数据),这个查询将为您提供当前月中每天最常见的风向:
select this_month.day,
(
select winddir
from weatherdatanum
where thedate >= this_month.day
and thedate < this_month.day + interval 1 day
group by winddir
order by count(*) desc
limit 1
) as daily_leader
from
(
select distinct date(thedate) as day
from weatherdatanum
where thedate >= concat(left(current_date(),7),'-01') - interval 1 month
) this_month发布于 2014-01-03 15:15:25
下面的查询应该返回一个风向列表以及按大多数事件排序的计数:
SELECT wind_dir, COUNT(wind_dir) AS count FROM `mytable` GROUP BY wind_dir ORDER DESC希望这有帮助
https://stackoverflow.com/questions/20906603
复制相似问题