简体   繁体   English

根据日期间隔汇总值

[英]Aggregate values based on a date interval

I have a database containing monthly precipitation values at some measurements locations. 我有一个数据库,其中包含一些测量位置的每月降水量值。 The structure of my table is: 我的表的结构是:

describe   pp_lunare;
Field      Type       Null
ID         int(11)    NO
DATA_OBS   date       NO
PLUTON     float      NO
LEGHIN     float      NO
DUMBRAVA   float      NO

A sample of my data: 我的数据样本:

ID    DATA_OBS     PLUTON    LEGHIN   DUMBRAVA
1    1977-01-01    14.4      33.3     25.1
2    1977-02-01    18.7      12.9     13.2
3    1977-03-01    32.8      26.7     18.3
4    1977-04-01    109.6     123.8    140.6
5    1977-05-01    98.5      104.7    59.9
6    1977-06-01    192.9     172.8    66.6
7    1977-07-01    101.4     85.8     79.4
8    1977-08-01    116.4     103.3    105.7
9    1977-09-01    54.5      47.4     51.8
10   1977-10-01    23.6      15.6     11
11   1977-11-01    59.7      44.3     29.7
12   1977-12-01    28.7      13.1     10

In my case I need to get the sum of the precipitation for every column at every 3 months something like this: 就我而言,我需要每3个月获取每一列的降水总和,如下所示:

ID   DATA_OBS      PLUTON    LEGHIN    DUMBRAVA
1    1977-03-01    65.9      72.9      56.6
2    1977-06-01    401       401.3     267.1
3    1977-09-01    272.3     236.5     247.9

and so on... 等等...

Thanks. 谢谢。

You can get the month from your date with month(data_obs) which returns a month from 1 to 12. Convert this to a value for the quarter by doing something like floor((month(data_obs) - 1)/3) as quarter to get quarters from 0 to 3. 您可以使用month(data_obs)从日期中获得月份,该月份返回一个从1到12的月份。通过执行floor((month(data_obs) - 1)/3) as quarter将其转换为该季度的值。从0到3获得四分之一。

For example, select data_obs, concat(year(data_obs), floor((month(data_obs) - 1)/3)) as quarter from pp_lunare; 例如, select data_obs, concat(year(data_obs), floor((month(data_obs) - 1)/3)) as quarter from pp_lunare; should show you both the original date and the derived quarter side by side. 应同时显示原始日期和衍生季度。

Then group by this new value and sum the rest: 然后将这个新值分组并求和其余的值:

select concat(year(data_obs), floor((month(data_obs) - 1)/3)) as quarter, sum(pluton), sum(leghin), sum(dumbrava)
from pp_lunare
group by quarter
order by quarter;

If you want the exact date format you had in the question, you'll have to do a little bit of string manipulation (add 1 to the quarter above, multiply that by 3, pad with 0s, then tack on a "-01" for the date). 如果要使用问题中的确切日期格式,则必须进行一些字符串操作(将1加至上述四分之一,将其乘以3,以0填充,然后加上“ -01”日期)。

You can get the month data at first, and convert it into [quarter], which is from 0 to 2. Then you can create three temporary tables given [quarter]=0,1,2, and the easy and final part will be to add up the i_th row in these tables, the result will be just like below. 您可以首先获取月份数据,然后将其转换为从0到2的[quarter]。然后您可以根据[quarter] = 0,1,2来创建三个临时表,其中简单和最后一部分将是在这些表中添加第i_th行,结果将如下所示。 And you can also have the data_obs data as well. 您也可以拥有data_obs数据。

ID DATA_OBS PLUTON LEGHIN DUMBRAVA 1 1977-03-01 65.9 72.9 56.6 2 1977-06-01 401 401.3 267.1 3 1977-09-01 272.3 236.5 247.9 ID DATA_OBS PLUTON LEGHIN DUMBRAVA 1 1977-03-01 65.9 72.9 56.6 2 1977-06-01 401 401.3 267.1 3 1977-09-01 272.3 236.5 247.9

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

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