2012-03-28 144 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