我有一个包含7列的表(测试),如下所示:
stcode | year | month | day | Rainfall| min_Temp| max_Temp|
+-------------+------+-------+------+---------+----
| chennai| 2013 | 1 | 1 | 10 | 23 | 28 |
| chennai| 2013 | 1 | 12 | 08 | 13 | 23 |
| chennai| 2013 | 1 | 18 | 04 | 15 | 25 |
| chennai| 2013 | 2 | 11 | 11 | 10 | 30 |
| chennai| 2013 | 2 | 23 | 18 | 12 | 22 |
| Madurai| 2013 | 1 | 08 | 24 | 14 | 24 |
| Madurai| 2013 | 1 | 19 | 09 | 19 | 29 |
| Madurai| 2013 | 1 | 01 | 22 | 11 | 21 |
| Trichy | 2013 | 2 | 17 | 06 | 16 | 26 |
| Trichy | 2013 | 2 | 21 | 16 | 26 | 36 |
| Trichy | 2013 | 2 | 27 | 26 | 18 | 28 |
......等等 我正试图获得'降雨量','min_temp','max_temp'的最小值和最大值以及相应的日期和按年,月分组 我使用像这样的查询
mysql> select * from test
-> where min_temp in(select min(min_temp) from test)
-> or min_temp in (select max(min_temp) from test)
-> or max_temp in (select min(max_temp) from test)
-> or max_temp in (select max(max_temp) from test)
-> group by stcode,year,month,day
-> order by stcode;
我正在尝试为每个月和相应的日期获得降雨量,最小值和最大值的最小值和最大值
答案 0 :(得分:1)
SQL基于逻辑设置...所以假设我理解要求:
1)返回任何stcode的最小/最大温度以及具有stcode最小/最大温度的所有日期。
所以首先得到一套符合您要求的条件。每个stcode每个月的最小和最大温度;如:
SELECT stcode, month, min(min_temp) as minTemp, max(min_temp) as maxTemp
FROM test
GROUP BY strCode, month
无论年复一日。
现在,如果你想知道发生了什么日子,只需加入原来的套装即可。
SELECT *
FROM test A
INNER JOIN (
SELECT stcode, month, min(min_temp) as minTemp, max(min_temp) as maxTemp
FROM test
GROUP BY strCode, month) B
on A.stcode = B.stCode
and A.MinTemp=B.MinTemp
and A.MaxTemp=B.MaxTemp
and A.Month=B.Month
所有匹配min / max temp和stcode的记录都会返回。
答案 1 :(得分:0)
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(stcode VARCHAR(12) NOT NULL
,date DATE NOT NULL
,Rainfall INT NOT NULL
,min_Temp INT NOT NULL
,max_Temp INT NOT NULL
,PRIMARY KEY(stcode,date)
);
INSERT INTO my_table VALUES
('chennai','2013-01-01',10,23,28),
('chennai','2013-01-12',08,13,23),
('chennai','2013-01-18',04,15,25),
('chennai','2013-02-11',11,10,30),
('chennai','2013-02-23',18,12,22),
('Madurai','2013-01-08',24,14,24),
('Madurai','2013-01-19',09,19,29),
('Madurai','2013-01-01',22,11,21),
('Trichy ','2013-02-17',06,16,26),
('Trichy ','2013-02-21',16,26,36),
('Trichy ','2013-02-27',26,18,28);
SELECT * FROM my_table;
+---------+------------+----------+----------+----------+
| stcode | date | Rainfall | min_Temp | max_Temp |
+---------+------------+----------+----------+----------+
| chennai | 2013-01-01 | 10 | 23 | 28 |
| chennai | 2013-01-12 | 8 | 13 | 23 |
| chennai | 2013-01-18 | 4 | 15 | 25 |
| chennai | 2013-02-11 | 11 | 10 | 30 |
| chennai | 2013-02-23 | 18 | 12 | 22 |
| Madurai | 2013-01-01 | 22 | 11 | 21 |
| Madurai | 2013-01-08 | 24 | 14 | 24 |
| Madurai | 2013-01-19 | 9 | 19 | 29 |
| Trichy | 2013-02-17 | 6 | 16 | 26 |
| Trichy | 2013-02-21 | 16 | 26 | 36 |
| Trichy | 2013-02-27 | 26 | 18 | 28 |
+---------+------------+----------+----------+----------+
SELECT stcode
, DATE_FORMAT(date,'%Y-%m')
, SUM(rainfall)
, MIN(min_temp)
, MAX(max_temp)
FROM my_table
GROUP
BY stcode
, DATE_FORMAT(date,'%Y-%m');
+---------+---------------------------+---------------+---------------+---------------+
| stcode | DATE_FORMAT(date,'%Y-%m') | SUM(rainfall) | MIN(min_temp) | MAX(max_temp) |
+---------+---------------------------+---------------+---------------+---------------+
| chennai | 2013-01 | 22 | 13 | 28 |
| chennai | 2013-02 | 29 | 10 | 30 |
| Madurai | 2013-01 | 55 | 11 | 29 |
| Trichy | 2013-02 | 48 | 16 | 36 |
+---------+---------------------------+---------------+---------------+---------------+
SELECT x.*
FROM my_table x
JOIN
( SELECT stcode,DATE_FORMAT(date,'%Y-%m') yearmonth, MIN(rainfall) min_rainfall FROM my_table GROUP BY stcode,DATE_FORMAT(date,'%Y-%m')) y
ON y.stcode = x.stcode
AND y.yearmonth = DATE_FORMAT(x.date,'%Y-%m')
AND y.min_rainfall = x.rainfall;
+---------+------------+----------+----------+----------+
| stcode | date | Rainfall | min_Temp | max_Temp |
+---------+------------+----------+----------+----------+
| chennai | 2013-01-18 | 4 | 15 | 25 |
| chennai | 2013-02-11 | 11 | 10 | 30 |
| Madurai | 2013-01-19 | 9 | 19 | 29 |
| Trichy | 2013-02-17 | 6 | 16 | 26 |
+---------+------------+----------+----------+----------+
答案 2 :(得分:0)
获得给定月份的最小值/最大值非常简单。更有问题的是返回发生最大值或最小值的那一天。
要在单行上获得结果,请执行以下操作:
SELECT m.stcode
, m.year
, m.month
, m.min_rainfall
, MIN(IF(d.rainfall=m.min_rainfall,d.day,NULL)) AS min_rainfall_day
, m.max_rainfall
, MIN(IF(d.rainfall=m.max_rainfall,d.day,NULL)) AS max_rainfall_day
, m.min_min_temp
, MIN(IF(d.min_temp=m.min_min_temp,d.day,NULL)) AS min_min_temp_day
, m.max_min_temp
, MIN(IF(d.min_temp=m.max_min_temp,d.day,NULL)) AS max_min_temp_day
, m.min_max_temp
, MIN(IF(d.max_temp=m.min_max_temp,d.day,NULL)) AS min_max_temp_day
, m.max_max_temp
, MIN(IF(d.max_temp=m.max_max_temp,d.day,NULL)) AS max_max_temp_day
FROM ( SELECT r.stcode
, r.year
, r.month
, MIN(r.rainfall) AS min_rainfall
, MAX(r.rainfall) AS max_rainfall
, MIN(r.min_temp) AS min_min_temp
, MAX(r.min_temp) AS max_min_temp
, MIN(r.max_temp) AS min_max_temp
, MAX(r.max_temp) AS max_max_temp
FROM test r
GROUP
BY r.stcode
, r.year
, r.month
) m
JOIN test d
ON d.stcode = m.stcode
AND d.year = m.year
AND d.month = m.month
GROUP
BY m.stcode
, m.year
, m.month
请注意,如果有超过一天(对于给定的stcode和月份)具有相同的“最高”max_temp,则此查询将仅返回发生的第一个“日期”。
例如,一个月内可能有多天有“0”降雨量。如果要返回所有那些日期值,可以使用GROUP_CONCAT()函数代替最外面的MIN(),以返回具有匹配值的逗号分隔的日期值列表。
, GROUP_CONCAT(IF(d.rainfall=m.min_rainfall,d.day,NULL)) AS min_rainfall_day_list