SQL:(Group by 中 )取某极值的其他数据项

/*案例: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 思路(特定思路)

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值