1、设定变量,表内连接
SET @baidu_date = 20161213;
SET @this_30_start = 20161016;
SET @this_30_stop = 20161114;
SET @last_180_start = 20160519;
SET @last_180_stop = 20161114;
SELECT
a.*, b.30_overall_QA,
b.30_mobile_QA
FROM
(
SELECT
id,
search_term,
create_time,
TRUNCATE (AVG(overall_trend), 0) 180_overall_QA,
TRUNCATE (AVG(pc_trend), 0) 180_pc_QA,
TRUNCATE (AVG(mobile_trend), 0) 180_mobile_QA
FROM
baidu_index_daily
WHERE
create_time = @baidu_date
AND index_date BETWEEN @last_180_start
AND @last_180_stop
GROUP BY
id
) a,
(
SELECT
id,
TRUNCATE (AVG(overall_trend), 0) 30_overall_QA,
TRUNCATE (AVG(mobile_trend), 0) 30_mobile_QA
FROM
baidu_index_daily
WHERE
create_time = @baidu_date
AND index_date BETWEEN @this_30_start
AND @this_30_stop
GROUP BY
id
) b
WHERE
a.id = b.id;
时间: 2024-09-30 18:58:55