我想做一个像这样的十大过滤器:
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
我想在这里做的是从“mock_table_1”查询A和SUM(SA),其中D在前10-D-成员中,而前10-D-成员表示字段D的成员它具有小的SUM(SD)
答案 0 :(得分: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
);
然后修改原始查询:
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;