我有一个这样的模型:
matrices (
matricesID integer;
x integer;
y integer;
value float;
)所以表中将存储许多矩阵数据,现在我需要逐边获取每个矩阵的平均值,即,如果一个矩阵是20 * 30,并且具有(5,3),(5,7),(5,15),(12,4),(17,5),(17,10)中的值,我需要获取四组数据,一组用于所有的x=5值,一组用于所有的x=17值,一组用于所有的y=4值,另一组用于y=15的所有值,因为它们是x和y的最大/最小值。
有没有办法用easy SQL来实现这一点呢?
任何想法都将受到感谢。
BR
爱德华一世
发布于 2012-03-28 23:56:07
这只是一个猜测,因为我在问题领域没有太多的经验:
select matricesID
, (select avg(value) from matrices where matricesID = a.matricesID and x = a.minx) as avgofminx
, (select avg(value) from matrices where matricesID = a.matricesID and x = a.maxx) as avgofmaxx
, (select avg(value) from matrices where matricesID = a.matricesID and y = a.miny) as avgofminy
, (select avg(value) from matrices where matricesID = a.matricesID and y = a.maxy) as avgofmaxy
from (
select matricesID
, min(x) as minx
, max(x) as maxx
, min(y) as miny
, max(y) as maxy
from matrices
group by matricesID
) as a这是在SQL Server中运行的,但语法非常简单,希望它能在您所使用的任何DBMS中运行
https://stackoverflow.com/questions/9910851
复制相似问题