/*案例:SQL:(Group by 中)取某极值的其他数据项
描述:取某极值的行的其他数据项。
如下:取每个广告位涉及单个游戏中 点击时间最大的 其他数据项(登录、激活数据等)
*/
--方法:子查询
select max(ccr.ad_click_time)/*极值*/,ccr.ad_space_code,ccr.ad_game_id,
(select top 1 B.active_count from stat_ads_continue_cookie_report B where B.ad_click_time=max(ccr.ad_click_time) and ccr.ad_space_code =B.ad_space_code and ccr.ad_game_id = B.ad_game_id )
--.....
--.....
from stat_ads_continue_cookie_report ccr
group by ccr.ad_space_code,ccr.ad_game_id
--方法2:摆脱Group by思路,使用row_number() OVER(PARTITION BY ...ORDER BY...) 达到目标
select A.ad_space_code,A.ad_game_id,A.active_count,A.login_count,A.number
from (
select *,row_number() OVER(PARTITION BY ad_space_code,ad_game_id ORDER BY ad_click_time desc ) number
from stat_ads_continue_cookie_report ) A
where A.number = 1
-- 方法:将方法2用CTE表达式
With ccr_stat_tb
AS
(
select ad_space_code,ad_game_id,active_count,login_count,row_number() OVER(PARTITION BY ad_space_code,ad_game_id ORDER BY ad_click_time desc) number
from stat_ads_continue_cookie_report
)
select * from ccr_stat_tb where number=1;
/*
总结:不要局限于Group by 思路(特定思路)
*/