首页 > 代码库 > 常用SQL记录

常用SQL记录

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;



















本文出自 “小小水滴” 博客,请务必保留此出处http://wangzan18.blog.51cto.com/8021085/1882706

常用SQL记录