暂无图片
暂无图片
暂无图片
暂无图片
1
暂无图片

GBase 8a MPP表字段分组后重复数据取其中一条的方法

原创 datamanage00001 2020-08-21
1793

1、表结构
gbase> desc tt1;
±------±-------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±-------------±-----±----±--------±------+
| id | int(11) | YES | | NULL | |
| name | varchar(100) | YES | | NULL | |
±------±-------------±-----±----±--------±------+
2 rows in set (Elapsed: 00:00:00.01)
2、表数据例子
gbase> select * from tt1;
±-----±-------+
| id | name |
±-----±-------+
| 2 | dddddd |
| 2 | 100 |
| 2 | aa |
| 3000 | ss |
| 3000 | ss |
| 3000 | ee |
| 3000 | ff |
| 3000 | 9999 |
| 3000 | aaa |
| 3000 | 10 |
±-----±-------+
10 rows in set (Elapsed: 00:00:00.03)
3、按ID字段分组,name取最大
gbase> select * from(select id,name,ROW_NUMBER() over(partition by id order by name desc ) num from tt1 )aa where num=1;
±-----±-------±----+
| id | name | num |
±-----±-------±----+
| 2 | dddddd | 1 |
| 3000 | ss | 1 |
±-----±-------±----+
2 rows in set (Elapsed: 00:00:00.12)
4、按ID字段分组,name取最小
gbase> select * from(select id,name,ROW_NUMBER() over(partition by id order by name ) num from tt1 )aa where num=1;
±-----±-----±----+
| id | name | num |
±-----±-----±----+
| 2 | 100 | 1 |
| 3000 | 10 | 1 |
±-----±-----±----+
2 rows in set (Elapsed: 00:00:00.14)
5、关于GBase与mysql、Oracle兼容性
在我们项目开发中发现,GBase语法与mysql语法相似度达99%,有mysq基础的伙伴使用gbase
上手很快;
与oracle语法相似度达90%,以上ROW_NUMBER() over(partition by id order by name )统计函数语法和Oracle一样

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

datamanage00001
暂无图片
4年前
评论
暂无图片 1
该方法对gbase/oracle等所有支持sql row_number()统计函数的数据库有效
4年前
暂无图片 1
评论