Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite-每组第一个-组合顺序和反向排序顺序_Sql_Sqlite_Greatest N Per Group - Fatal编程技术网

SQLite-每组第一个-组合顺序和反向排序顺序

SQLite-每组第一个-组合顺序和反向排序顺序,sql,sqlite,greatest-n-per-group,Sql,Sqlite,Greatest N Per Group,我正在寻找关于如何在SQLite中选择每个组的第一条记录的选项,在SQLite中,组的排序是通过复合键进行的 示例表: Key_1 | Sort1 | Sort2 | Val_1 | Val_2 -------+-------+-------+-------+------- 1 | 1 | 3 | 0 | 2 1 | 1 | 2 | 2 | 4 1 | 1 | 1 | 4 | 6

我正在寻找关于如何在SQLite中选择每个组的第一条记录的选项,在SQLite中,组的排序是通过复合键进行的

示例表:

 Key_1 | Sort1 | Sort2 | Val_1 | Val_2
-------+-------+-------+-------+------- 
   1   |   1   |   3   |   0   |   2
   1   |   1   |   2   |   2   |   4
   1   |   1   |   1   |   4   |   6
   1   |   2   |   2   |   6   |   8
   1   |   2   |   1   |   8   |   1
   2   |   1   |   2   |   0   |   5
   2   |   1   |   1   |   1   |   6
   2   |   2   |   3   |   2   |   7
   2   |   2   |   2   |   3   |   8
   2   |   2   |   1   |   4   |   9
目标: -按键1 ASC、Sort1 ASC、Sort2 DESC对数据进行排序 -按唯一键选择第一条记录\u 1

解析函数解

 SELECT
    *
 FROM
 (
    SELECT
        *,
        ROW_NUMBER() OVER (PARTITION BY Key_1
                               ORDER BY Sort1,
                                        Sort2 DESC
                          )
                              AS group_ordinal
    FROM
        table
 )
     sorted
 WHERE
     group_ordinal = 1
费力的ANSI-92方法

SELECT
    table.*
FROM
    table
INNER JOIN
(
    SELECT
        table.Key1, table.Sort1, MAX(table.Sort2) AS Sort2
    FROM
        table
    INNER JOIN
    (
        SELECT
            Key_1, MIN(Sort1)
        FROM
            table
        GROUP BY
            Key_1
    )
        first_Sort1
            ON  table.Key_1 = first_Sort1.Key_1
            AND table.Sort1 = first_Sort1.Sort1
    GROUP BY
        table.Key1, table.Sort1
)
    first_Sort1_last_Sort2
        ON  table.Key_1 = first_Sort1_last_Sort2.Key_1
        AND table.Sort1 = first_Sort1_last_Sort2.Sort1
        AND table.Sort2 = first_Sort1_last_Sort2.Sort2
这涉及大量嵌套和自联接。当它只涉及两个排序列时,这就足够麻烦了

我的实际示例有六个排序列

我也希望避免以下情况,因为据我所知,这不是保证/确定的

SELECT
    table.*
FROM
    table
GROUP BY
    table.Key_1
ORDER BY
    MIN(table.Sort1),
    MAX(table.Sort2)

还有其他我没有看到的选项吗?

我相信这会在SQLite中起作用:

select t.*
from table t
where exists (select 1
              from (select t2.*
                    from table t2
                    where t2.id = t.id
                    order by t2.sort1 asc, t2.sort2 desc
                    limit 1
                   ) t2
              where t2.sort1 = t.sort1 and t2.sort2 = t.sort2
             );
我关心的是SQLite是否允许嵌套子查询中的相关引用。如果不是,则可以使用=并将这些值连接在一起:

select t.*
from table t
where (sort1 || ':' || sort2) =
          (select (sort1 || ':' || sort2)
           from table t2
           where t2.id = t.id
           order by sort1 asc, sort2 desc
           limit 1
          );

很好,两个查询都给出了WHERE a,b=从x中选择a,b,但都是SQLite兼容的。我会让他们两个都试一试,然后绞尽脑汁考虑哪一个选项是最容易混淆的,因为不管可怜的戴夫要处理什么,这都是一年的时间。@MatBailie。虽然我对第二种解决方案不感兴趣,但对于第一次阅读查询的人来说,这可能更清楚。使用第一种解决方案是为了避免隐式类型转换的问题。然后,通过类似于分析函数中的分区和orderby,添加了关于相关性和顺序的明智注释。有一个有趣的限制。外部查询的字段可以在WHERE子句中引用,但不能在ORDER BY中引用。不确定这是否意味着这是不受支持的功能。改天会挖得更深。
select t.*
from table t
where (sort1 || ':' || sort2) =
          (select (sort1 || ':' || sort2)
           from table t2
           where t2.id = t.id
           order by sort1 asc, sort2 desc
           limit 1
          );