2016-07-22 75 views
0

我想要做一個排名前10位過濾器是這樣的:如何在Monetdb中實現top n過濾器?

SELECT t0."A" AS d0, 
    t0."B" AS d1, 
    t0."C" AS d2, 
    t0."D" AS d3, 
    SUM(t0."SA") AS m0, 
    SUM(t0."SB") AS m1 
FROM "mock_table_1" AS t0 
INNER JOIN (     //the top 10 filter begin here 
    SELECT t0."D" AS fd, 
    SUM(t0."SD") AS top 
    FROM "mock_table_1" AS t0 
    GROUP BY t0."D" 
    ORDER BY top ASC 
    LIMIT 10 
) AS p0u1 
ON (t0."D" = p0u1.fd)   //the top 10 filter end here 
GROUP BY t0."A", 
    t0."B", 
    t0."C", 
    t0."D" 
HAVING (SUM(t0."X") <= 100000000) 
LIMIT 100 

但由於order-by in subquery not avaliable in Monetdb?

所以不工作,我應該怎樣做來實現這個前n個過濾器?


簡化SQL例如:

SELECT t0."A" AS d0, 
    SUM(t0."SA") AS m0 
FROM "mock_table_1" AS t0 
INNER JOIN (     //the top 10 filter begin here 
    SELECT t0."D" AS fd, 
    SUM(t0."SD") AS top_cond 
    FROM "mock_table_1" AS t0 
    GROUP BY t0."D" 
    ORDER BY top_cond ASC 
    LIMIT 10 
) AS top_filter 
ON (t0."D" = top_filter.fd)  //the top 10 filter end here 
GROUP BY t0."A" 
LIMIT 100 

我想在這裏做的是A和SUM(SA)從 「mock_table_1」,其中d是在頂部的10 d-會員查詢,和頂10 d-成員是指具有的smallist SUM(SD)

+0

start here? http://stackoverflow.com/questions/30641876/monetdb-sql-method-to-locate-or-match-by-the-nearest-value-without-a-top-or-lim –

+0

我可以給你一個漂亮的簡單的答案,但首先 - 請使用_minimal_例子,即您可以想到的最簡單的表格和查詢,您可以爲其構建「top n filter」查詢。 – einpoklum

+0

@einpoklum對不起,我給出了這個查詢,因爲我認爲它足夠簡單了,我可以簡化它,如果你想。 – luochen1990

回答

0

因此,創建功能,這是否返回一個支配(由子查詢限制)表

CREATE FUNCTION my_cheating_function() 
    RETURNS TABLE (fd [data type for fd], top [data type for top]) 
    RETURN TABLE (
     SELECT 
      t0."D" AS fd, 
      SUM(t0."SD") AS top 
     FROM 
      "mock_table_1" AS t0 
     GROUP BY 
      t0."D" 
     ORDER BY 
      top ASC 
     LIMIT 10 
    ); 
字段d的成員

然後修改原始查詢:

SELECT t0."A" AS d0, 
    t0."B" AS d1, 
    t0."C" AS d2, 
    t0."D" AS d3, 
    SUM(t0."SA") AS m0, 
    SUM(t0."SB") AS m1 
    FROM 
    "mock_table_1" AS t0 
    INNER JOIN 
    my_cheating_function() AS p0u1 
    ON 
    (t0."D" = p0u1.fd)   
    GROUP BY 
    t0."A", 
    t0."B", 
    t0."C", 
    t0."D" 
    HAVING (SUM(t0."X") <= 100000000) 
    LIMIT 100; 

這很可能也可以通過創建該子查詢視圖,並以類似的方式如上加入的視圖來完成。僅供參考創建該視圖執行以下操作:

CREATE VIEW my_cheating_view AS 
    SELECT 
     t0."D" AS fd, 
     SUM(t0."SD") AS top 
    FROM 
     "mock_table_1" AS t0 
    GROUP BY 
     t0."D" 
    ORDER BY 
     top ASC 
    LIMIT 10;