在SQL

时间:2017-09-21 14:05:54

标签: sql sql-server

select  
logs.uid,
logs.event_value

from publicview.treasurehunters_ok logs
left join public.app_ok_cbaiodclebabababa_agg_users users  on logs.uid=users.uid
where  first_event_time>='01.09.2017 0:00:00'
and first_event_time <= '11.09.2017 0:00:00'
and logs.event ='balance_OK'

我需要按时间按升序对每个uid进行排序,并只保留logs.event_value的第一个值

uid  logs.event_value
1      10
1      5
1      3

我们只需要返回 1 10即离开第一行 为每个用户。 所以可以制作?

1 个答案:

答案 0 :(得分:1)

您只需要选择一个ID,最大日期权限正确吗?您可以运行partition by,如下所示:

select * from (
select  
logs.uid,
logs.event_value,
ROW_NUMBER() OVER (PARTITION BY uid ORDER BY first_event_time DESC) r 

from publicview.treasurehunters_ok logs
left join public.app_ok_cbaiodclebabababa_agg_users users  on 
logs.uid=users.uid
where  first_event_time>='01.09.2017 0:00:00'
and first_event_time <= '11.09.2017 0:00:00'
and logs.event ='balance_OK') tab
where r=1