如何合并我的数据以减少MariaDB中不同值的数量?

时间:2019-06-17 11:28:17

标签: sql mariadb rounding common-table-expression

我有风速数据,每秒有一个记录。我发现,95%+的数据是0.36 m / s的倍数(请参见屏幕快照Wind Speeds),我希望发现这是由于我们的仪器配置方式造成的(仍然努力工作。)

我想做的是将所有值向上或向下取整为0.36的倍数,这样我就可以生成概率密度视觉图像。

屏幕截图中的表格来自以下代码:

WITH freqCTE AS
    (
    SELECT rd.wind_speed,
        COUNT(rd.wind_speed) AS AbsFreq,
        CAST(ROUND(100. * (COUNT(rd.wind_speed)) / (SELECT COUNT(*) FROM raw_data), 3) AS DEC(4,3)) AS AbsPerc
        FROM raw_data AS rd
        GROUP BY rd.wind_speed
    )
SELECT wind_speed, AbsFreq,
    SUM(AbsFreq) OVER(ORDER BY wind_speed ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CumFreq,
    AbsPerc,
    SUM(AbsPerc) OVER(ORDER BY wind_speed ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CumPerc,
    CAST(REPEAT('.',AbsPerc*100) AS VARCHAR(200)) AS Histogram
    FROM freqCTE
    ORDER BY wind_speed;

我知道我可以像这样绕风速:

ROUND(rd.wind_speed / 0.36, 0) * 0.36

我已经尝试过CTE内部的各种事情,并且在CTE之后尝试使用SET,但我只是无法让它做我想做的事情。我真正想要看到的是一张类似这样的表:

ws36     freq
0.00     4000
0.36     500
0.72     600
1.08     800
etc.

1 个答案:

答案 0 :(得分:0)

我正在考虑一个更简单的查询:

SELECT ROUND(rd.wind_speed / 0.36, 0) * 0.36 as wind_speed_036,
       COUNT(*) as cnt
FROM raw_data  rd
GROUP BY wind_speed_036
ORDER BY wind_speed_036;