Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql中按45分组_Mysql_Sql_Group By - Fatal编程技术网

在mysql中按45分组

在mysql中按45分组,mysql,sql,group-by,Mysql,Sql,Group By,我有一个数据库,在其中我按度数存储风向读数。我需要做两件事,真的,我需要把所有读数按45度分组,基本上是在N,NE,E,SE,S,SW,W和NW之间,然后我需要找到这些组在所有读数中的百分比 这就是我迄今为止所遇到的问题,我只能通过阅读来分组,而忽略了如何按45分组的部分,并成功地显示了百分比 select reading_winddirection, count, round(count / total * 100) postotak from (

我有一个数据库,在其中我按度数存储风向读数。我需要做两件事,真的,我需要把所有读数按45度分组,基本上是在N,NE,E,SE,S,SW,W和NW之间,然后我需要找到这些组在所有读数中的百分比

这就是我迄今为止所遇到的问题,我只能通过阅读来分组,而忽略了如何按45分组的部分,并成功地显示了百分比

select 
    reading_winddirection, 
    count, 
    round(count / total * 100) postotak 
from 
    ( 
        select reading_winddirection, count(reading_winddirection) count 
        from simulation_readings  
        group by reading_winddirection 
    ) c 
    JOIN ( 
        select count(*) total from simulation_readings  
    ) t
这是我的小提琴:

在过去的30分钟里,我已经设法找出了如何按数字范围进行填充,所以这是我问题的一半:

SELECT `reading_winddirection`, 
   Count(*) AS num 
FROM   (SELECT CASE 
             WHEN `reading_winddirection` >= 1 
                  AND `reading_winddirection` < 45 THEN '0-45' 
             WHEN `reading_winddirection` >= 45 
                  AND `reading_winddirection` < 90 THEN '45-90' 
             WHEN `reading_winddirection` >= 90 
                  AND `reading_winddirection` < 135 THEN '90-135' 
             WHEN `reading_winddirection` >= 135 
                  AND `reading_winddirection` < 180 THEN '135-180' 
             WHEN `reading_winddirection` >= 180 
                  AND `reading_winddirection` < 225 THEN '180-225' 
             WHEN `reading_winddirection` >= 225 
                  AND `reading_winddirection` < 270 THEN '225-270' 
             WHEN `reading_winddirection` >= 270 
                  AND `reading_winddirection` < 315 THEN '270-315' 
             WHEN `reading_winddirection` >= 315 
                  AND `reading_winddirection` < 360 THEN '315-360' 
           end AS `reading_winddirection` 
    FROM   simulation_readings 
    WHERE  1) AS price_summaries 
GROUP  BY reading_winddirection 

我只需要找出计数中所有组的百分比*

一个简单的解决方案是将reading\u winddirection除以45,只保留整数部分,然后使用它进行分组

select 
    case c.windgroup
        when 0 then 'N'
        when 1 then 'NE'
        when 2 then 'E'
        when 3 then 'SE'
        when 4 then 'S'
        when 5 then 'SW'
        when 6 then 'W'
        when 7 then 'NW'
    end windgroup,
    cnt,
    round(c.cnt / t.total * 100) postotak 
from 
    ( 
        select floor(reading_winddirection / 45) windgroup, count(*) cnt
        from simulation_readings  
        group by windgroup
    ) c 
    cross join ( 
        select count(*) total from simulation_readings  
    ) t
注意:如果您使用的是MySQL 8.0,则可以使用窗口函数而不是联接来计算总计数:

select 
    case floor(reading_winddirection / 45)
        when 0 then 'N'
        when 1 then 'NE'
        when 2 then 'E'
        when 3 then 'SE'
        when 4 then 'S'
        when 5 then 'SW'
        when 6 then 'W'
        when 7 then 'NW'
    end windgroup,
    count(*) cnt,
    round(100 * count(*) / sum(count(*)) over()) postotak 
from simulation_readings  
group by windgroup
在中,两个查询都产生:

windgroup | cnt | postotak :-------- | --: | -------: N | 6 | 60 E | 2 | 20 SE | 1 | 10 SW | 1 | 10
谢谢你的小提琴。请同时向我们展示您期望的结果。@GMB我已经用更多细节更新了我的问题非常不清楚的问题,很抱歉,即使风组的行数为0,也可以显示所有风组吗?