KingbaseES 中实现mysql any_value 功能

KingbaseES 中可以通过构造一个聚集函数来实现mysql的any_value功能。

示例

创建函数
create function coalesce_("anyelement","anyelement") returns "anyelement"
	language sql as $$ select coalesce( $1,$2) $$;
	
create aggregate any_value("anyelement")(sfunc=coalesce_, stype="anyelement");

测试
test=# select * from student;
 id | name | course | score 
----+------+--------+-------
  1 | 张三 | 语文   |    89
  2 | 张三 | 数学   |    99
  3 | 张三 | 外语   |    84
  4 | 张三 | 物理   |    77
  5 | 张三 | 化学   |    87
  6 | 李四 | 语文   |    91
  7 | 李四 | 数学   |    81
  8 | 李四 | 外语   |    88
  9 | 李四 | 物理   |    68
 10 | 李四 | 化学   |    83
 11 | 王五 | 语文   |    85
 12 | 王五 | 数学   |    65
 13 | 王五 | 外语   |    95
 14 | 王五 | 物理   |    90
 15 | 王五 | 化学   |    78
(15 行记录)

test=# select any_value(id),sum(score),name from student group by name;  
 any_value | sum | name 
-----------+-----+------
        11 | 413 | 王五
         1 | 436 | 张三
         6 | 411 | 李四
(3 行记录)
posted @   阿白*  阅读(188)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示