Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 单个查询中ID子集函数的最大值?_Sql_Postgresql_Max_Aggregate Functions_Aggregation - Fatal编程技术网

Sql 单个查询中ID子集函数的最大值?

Sql 单个查询中ID子集函数的最大值?,sql,postgresql,max,aggregate-functions,aggregation,Sql,Postgresql,Max,Aggregate Functions,Aggregation,如何在单个查询中生成以下代码: 节点:id,value i、 e.根据整个表返回应用于节点子集的函数Max的Max 这是伪代码。 数据库是postgresql #select the nodes, filtered by some criteria Nodes = select id,value from nodes where .... #for every node.value find the max of fun() applied to the whole table, collec

如何在单个查询中生成以下代码:

节点:id,value

i、 e.根据整个表返回应用于节点子集的函数Max的Max

这是伪代码。 数据库是postgresql

#select the nodes, filtered by some criteria
Nodes = select id,value from nodes where ....

#for every node.value find the max of fun() applied to the whole table, collect it
FOR n IN Nodes :
  Maxes.append( 
     select s.id, MAX(fun(n.value, s.value)) 
     from nodes s 
     where s.id != n.id
  )

#find the Max-score&Id of the collected Max scores
ID,score = MAX(Maxes)
在sql中,您只需调用max(),它是一个聚合函数,并针对提供给它的整个数据集运行,因此您的查询将非常简单:

select max(fun(n1.value, n2.value))
from nodes n1
join nodes n2
 on n1.id <> n2.id 
where ....
选择最大值(乐趣(n1.值,n2.值))
来自节点n1
连接节点n2
关于n1.id n2.id
哪里

当然,您需要在postgresl的样本数据、所需结果和适当的数据库标记中预先定义并使用fun()函数。这是什么语言?我觉得它不像SQL。谢谢。。fun()返回的是分数,而不是向量。fun()依赖于两个向量。每个选定节点都有自己的最大分数,我必须从这些最大分数中选择最大值。。从某种意义上说,它在一个循环中的循环不是这个连接只会在“十字”上达到最大值。。它必须为每个选定节点对整个表应用fun()。@sten,然后删除where子句。总之,问题中提供了这一点信息,这似乎回答了问题,否则您必须提供示例数据和所需的输出以及完整的解释