I have one table with columns that contains data about wind (wind,gusts, and direction)
I want to get average for wind and gust and most common value for direction for selected time period
I can manage to do it in two queries but don't know how to merge them
SELECT
avg(wind) as wind,
avg(gust) as gust
FROM
station
WHERE
id >='2018-10-20 00:00:00' AND id <'2018-10-20 04:00:00'
SELECT
direction,
count(direction) as sm
FROM
station
WHERE
id >='2018-10-20 00:00:00' AND id <'2018-10-20 04:00:00'
GROUP BY
direction
ORDER BY
sm DESC LIMIT 1
i tried
SELECT
avg(wind) as wind,
avg(gust) as gust,
direction,
count(direction) as sm
FROM
station
WHERE
id >='2018-10-20 00:00:00' AND id <'2018-10-20 04:00:00'
GROUP BY
direction
ORDER BY
sm DESC LIMIT 1
but this will calculate average for data but only where direction is most common value. Help is appreciated
You can try this.
SELECT direction,
t1.wind,
t1.gust,
count(direction) as sm
FROM station CROSS JOIN (
SELECT avg(wind) as wind,
avg(gust) as gust
FROM station
WHERE id >='2018-10-20 00:00:00' AND id <'2018-10-20 04:00:00'
) t1
WHERE id >='2018-10-20 00:00:00' AND id <'2018-10-20 04:00:00'
GROUP BY direction
ORDER BY sm DESC
LIMIT 1
or you can use subquery in select
SELECT direction,
(select avg(wind) FROM station) wind,
(select avg(gust) FROM station) gust,
count(direction) as sm
FROM station
WHERE id >='2018-10-20 00:00:00' AND id <'2018-10-20 04:00:00'
GROUP BY direction
ORDER BY sm DESC
LIMIT 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.