I want to do a top 10 filter like this:
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
but this doesn't work since order-by in subquery not avaliable in Monetdb?
So, what should I do to implement this top n filter ?
The simplified SQL example:
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
What I want to do here is to query A and SUM(SA) from "mock_table_1" where D is in the top-10-D-members, and the top-10-D-members means the members of field D which has the smallist SUM(SD)