row_number() over()过滤去重

一个id对应多个城市,按城市排序,使用row_number() over()过滤去重的方式,只取排在第一的一条

insert overwrite table xxxx partition(ds=%YYYYMMDD%)
select
 		count(distinct id) as total_trace_user  --
		,sum(xx_time + oo_time) as xx_oo_time   --
		,sum(total_time) as total_time   --
		,sum(distance) as total_distance			--
		,sum(xx_distance) as xx_distance		--
		,sum(oo_distance) as oo_distance 	--
		,IF(grouping(osv)==1,'all',osv) AS osv
		,IF(grouping(city)==1,'all',city) AS city
		,IF(grouping(channel)==1,'all', channel ) AS channel
        ,count(distinct (case when xx_distance > 0 then  id end)) as navi_uv	--
		,count(distinct (case when oo_distance > 0 then  id end)) as cruise_uv --
        ,count(distinct (case when oo_distance > 0 and xx_distance > 0 then  id end)) as repeate_uv  --
		,sum(xx_time) as xx_time
		,sum(oo_time) as oo_time
        ,IF(grouping(province)==1,'all', province ) AS province
        ,sum(sid_num) as  total_trace   --
	from
			(
            select 
                id 
                ,sid_num
                ,distance
                ,xx_distance
                ,oo_distance
                ,total_time
                ,xx_time
                ,oo_time
                ,channel 
                ,city
				,osv
				,province
			from (
				select  *
						,row_number() over(partition by id order by city) as num  --
				from		
					xxxxx
				where
					ds = %YYYYMMDD%
				) t
			where t.num= 1
            )a
	group by
		osv
        ,city
        ,channel
		,province
	with cube


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值