[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>sql字段去重
在从数据库中取数据或者进行数据清洗的过程中,经常会遇到去重复问题,常用的去重方法包括三种方式。接下来主要介绍常用的三种方式以及最优方法。
* distinct
* group by
* row_number() over()
<>需求分析
对下表中uid字段去重,并且把去重后所有字段输出。
---
<>distinct方法
* ditinct方法适合于单字段去重,但是单字段去重还要保留其他字段数据,就无法完成了;
* distinct对NULL是不进行过滤的,即返回的结果中是包含NULL值的;
* distinct应用到多个字段的时候,distinct必须放在开头,其应用的范围是其后面的所有字段 ---
<>group by 方法
* 对group by 后面所有字段去重,并不能只对一列去重;
* sql语句写成只对一列去重,保留其他字段,在hive上会报错
报错结果如下:
---
<>row_number() over()(该方法是hive sql去重的最佳方法)
* ROW_Number() over (partition by id order by time DESC)
给分组后的每个id加一列按某一字段倒叙排序的rank值,取rank=1 SELECT aa.uid,aa.org_name,aa.addr_no,
row_number() over (partition by aa.uid order BY aa.addr_no) as rn FROM stg.
t_018_doc_address_spec_organizationAS aa ) AS t WHERE t.rn =1
上述方法去重结果如下:
---