SQLite3中的窗口函数

时间:2018-03-02 12:58:57

标签: sql sqlite window-functions

以下Oracle SQL select允许我根据某些字段选择 duplicated 的表的所有行,例如,它们具有相同的COLUMN_1COLUMN_2COLUMN_3

SELECT  *
FROM    (
        SELECT  t.*, ROW_NUMBER() OVER (PARTITION BY COLUMN_1, COLUMN_2, COLUMN_3 ORDER BY COLUMN_1) AS rn
        FROM    MY_TABLE t
        )
WHERE   rn > 1;

如何在sqlite3中做同样的事情?

2 个答案:

答案 0 :(得分:1)

您可以使用private class MyModel extends BaseObservable { private String field1; private String field2; @Bindable public String getField1() { return this.field1; } @Bindable public String getField2() { return this.field2; } public void setField1(String firstName) { this.field1 = firstName; notifyPropertyChanged(BR.field1); } public void setField2(String lastName) { this.field2 = lastName; notifyPropertyChanged(BR.field2); } } 和相关子查询:

rowid

如果表格中有一个,您可以使用任何唯一键而不是select t.* from (select t.*, (select count(*) from my_table t2 where t2.column_1 = t.column_1 and t2.column_2 = t.column_2 and t2.column_3 = t.column_3 and t2.rowid <= t.rowid ) as seqnum from my_table t ) t where seqnum > 1;

答案 1 :(得分:1)

以下查询可以让您接近您想要的内容:

SELECT t1.*
FROM MY_TABLE t1
LEFT JOIN
(
    SELECT COLUMN_2, COLUMN_3, MIN(COLUMN_1) AS MIN_COLUMN_1
    FROM MY_TABLE
    GROUP BY COLUMN_2, COLUMN_3
) t2
    ON t1.COLUMN_2 = t2.COLUMN_2 AND
       t1.COLUMN_3 = t2.COLUMN_3 AND
       t1.COLUMN_1 = t2.MIN_COLUMN_1
WHERE
    t2.COLUMN_2 IS NULL;

此查询的工作原理是过滤掉每个COLUMN_1COLUMN_2组的最小COLUMN_3值的记录。实际上,这对应于排名而不是行号,但这可能是您可以接受的。