Запрос можно покороче:
select
t1.epoch,
t1.value,
t1.message,
(select `value` from data where epoch < t1.epoch and message = "isig_level" ORDER BY epoch DESC LIMIT 1) prev_value,
(select `value` from data where epoch > t1.epoch and message = "isig_level" ORDER BY epoch LIMIT 1) next_value
from data t1
where t1.value = 0 and t1.message = "isig_level"
and prev_value <> 0 and next_value <> 0
order by t1.epoch
@vvzvlad
Что не так с этим запросом:
select
t2.epoch,
t2.`key`,
t2.`value`
from
(
select
t1.`value`,
t1.`key`,
t1.epoch,
t1.message,
(select `value` from data where epoch < t1.epoch and message = "isig_level" ORDER BY epoch DESC LIMIT 1) prev_value,
(select `value` from data where epoch > t1.epoch and message = "isig_level" ORDER BY epoch LIMIT 1) next_value
from data t1
where t1.value = 0 and t1.message = "isig_level"
order by t1.epoch
) t2
where t2.prev_value = 0 and t2.next_value = 0
Алексей:
Правильно, второй join объединяет алиасы x и у, а в условии джойна ON нет х и y, а есть O и y: y ON O.Id = y.Order_id.
Ошибка будет логическая, в подсчетах sum.
IMHO сначала нужно сделать джойн O и x, а потом РЕЗУЛЬТАТ сджойнить с y.
Посмотрите в моем запросе выше.
Александр Рублев:
Этот запрос будет работать неправильно.
Объясню: в этом запросе второй join объединяет алиасы x и у, а в условии джойна ON заданы: O и у. Это неправильно, будут ошибки из-за неправильного объединения.
select
t5.Continent,
t5.Region,
sum(t5.Countries),
sum(t5.LifeDuration),
sum(t5.Population),
sum(t5.Cities),
sum(t5.Languages)
from
(
select
t3.Code
t3.Continent,
t3.Region,
t3.Countries,
t3.LifeDuration,
t3.Population,
t3.Cities,
t4.Languages
from
(
select
t1.Code,
t1.Continent,
t1.Region,
t1.Countries,
t1.LifeDuration,
t1.Population,
t2.Cities
from
(
select
Code,
Continent,
Region,
count(Name) as Countries,
ROUND(AVG(LifeExpectancy), 2) as LifeDuration,
SUM(Population) as Population
from country
group by Code,Continent,Region
) t1 left join
(
select
CountryCode,
count(Name) as Cities
from city
group by CountryCode
) t2 on t2.CountryCode = t1.Code
) t3 left join
(
select
CountryCode,
count(Language) as Languages
from countrylanguage
) t4 on t4.CountryCode = t3.Code
) t5
group by t5.Continent,t5.Region
Code нельзя убирать.
После выполнения моего запроса, у вас уже есть все данные, теперь их нужно сгруппировать (без Code):
select
t3.Continent,
t3.Region,
sum(t3.Countries),
sum(t3.LifeDuration),
sum(t3.Population),
sum(t3.Cities),
sum(t4.Languages)
from
(
select
t1.Code,
t1.Continent,
t1.Region,
t1.Countries,
t1.LifeDuration,
t1.Population,
t2.Cities
from
(
select
Code,
Continent,
Region,
count(Name) as Countries,
ROUND(AVG(LifeExpectancy), 2) as LifeDuration,
SUM(Population) as Population
from country
group by Code,Continent,Region
) t1 left join
(
select
CountryCode,
count(Name) as Cities
from city
group by CountryCode
) t2 on t2.CountryCode = t1.Code
) t3 left join
(
select
CountryCode,
count(Language) as Languages
from countrylanguage
) t4 on t4.CountryCode = t3.Code
SELECT t1.post_id, SUM(t1.dl)
FROM wp_day_download t1
INNER JOIN
( SELECT DISTINCT object_id FROM wp_term_relationships t2
INNER JOIN wp_term_taxonomy t3 ON t3.term_taxonomy_id = t2.term_taxonomy_id AND t3.term_id = 179
) t4 ON t4.object_id = t1.post_id
WHERE t1.date >= DATE_ADD(CURRENT_TIMESTAMP,INTERVAL -7 DAY)
GROUP BY t1.post_id
Шустро проходим мимо.