2012-03-28 143 views
1

处理我有这样一个模型:多稀疏矩阵与SQL

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。

有没有什么办法可以用简单的SQL来执行此操作?

任何想法将不胜感激。

BR

爱德华 我

回答

0

这是一个猜测,我没有在问题领域很多经验:

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中

+0

非常感谢你。 – user729544 2012-03-29 22:29:53