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

时间:2015-06-04 10:50:24

标签: sql monetdb

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

如果我可以使用TOP那么我相信这个命令会给我我想要的东西。有没有合理的替代方案,效率不高?我需要在一个快速最大化我的服务器内存的桌子上运行它。谢谢!

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 

1 个答案:

答案 0 :(得分:0)

来自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;

了解实际模式有助于了解每个事件是否&#34;由yr,person_id标识 (如上所述)或者说,(yr,person_id,svcmon)(在这种情况下应该添加e.svcmon) 分区依据)。

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

最佳, 斯蒂芬