monetdb sql 方法来定位或匹配最接近的值,没有 TOP 或 LIMIT

monetdb sql method to locate or match by the nearest value, without a TOP or LIMIT

我正在尝试在 monetdb 中复制 this question,我认为它不支持 TOP 命令并且只能在最外层结果上使用 LIMIT。

如果我可以使用 TOP 那么我相信这个命令会给我想要的东西。有没有效率不高的合理替代方案?我需要 运行 在 table 上进行此操作,这将很快耗尽我服务器的内存。谢谢!

CREATE TABLE nearest_matches AS 
    ( SELECT a.* ,  
        (
            SELECT TOP 1 svcmon
            FROM person_table AS z
            WHERE a.yr = z.yr AND a.person_id = z.person_id
            ORDER BY abs( z.svcmon - a.svcmon )
        ) AS nearest_month
    FROM event_table AS a ) WITH DATA 

来自 CWI 的 Stefan Manegold

嗨,

根据我对所需语义的理解提出我的建议:

原题:

create table a (id int, sales int, date timestamp);
create table b (id int, goal int, date timestamp);
select a.*, b.* from a,b, (select a.date as a_date, max(b.date) as b_date from a,b where b.date < a.date group by a.date) as ab where a.date = ab.a_date and b.date = ab.b_date;

下面的问题:

create table event_table (yr int, person_id int, svcmon int, payload string);
create table person_table (yr int, person_id int, svcmon int, payload string);
select * from (select e.yr, e.person_id, e.svcmon as e_svcmon, e.payload as e_payload, p.svcmon as p_svcmon, p.payload as p_payload, row_number() over (partition by e.yr,e.person_id order by abs(e.svcmon - p.svcmon) asc) as pos from event_table e , person_table p where e.yr = p.yr and e.person_id = p.person_id) as ep where pos = 1;

了解实际模式将有助于理解 "each event" 是否由 yr,person_id 标识 (正如我上面假设的那样)或者说, (yr,person_id,svcmon) (在这种情况下 e.svcmon 应该添加到 partition-by 子句)。

了解实际模式也可能有助于将投影从内部查询中拉出, 从而缩小中间结果大小...

最好的, 斯特凡