简体   繁体   English

初始化SortOrder列(例如0,1,2,3)的最佳方法是什么,其中有多个组基于另一个字段?

[英]What is the best way to initialize a SortOrder column (e.g. 0, 1, 2, 3) where there are multiple groups based on another field?

I have a table of list items. 我有一个列表项目表。 There is a ListID column used as an identifier to group the list items together. 有一个ListID列用作标识符,用于将列表项组合在一起。 Is there a sane way to give every item a sort order, starting at 0 per list and incremental by one per item. 是否有一种理智的方式为每个项目提供排序顺序,从每个列表0开始,每个项目增加1。

Basically, I need to populate the following SortOrder Column values for a large number of entries/ListIDs. 基本上,我需要为大量条目/ ListID填充以下SortOrder列值。

ID    ListID    SortOrder
1     1         0
2     0         0
3     1         1
4     0         1
5     1         2
6     0         2
7     2         0
8     2         1
9     2         2

You can use ROW_NUMBER() with a PARTITION on the ListId field for this: 你可以在ListId字段上使用带有PARTITION ROW_NUMBER()

Select   Id, ListId, 
         Row_Number() Over (Partition By ListId Order By Id) -1 As SortOrder
From     YourTable
Order By Id

I think you want: 我想你想要:

WITH toupdate as (
      SELECT t.*,
             ROW_NUMBER() OVER (PARTITION BY ListId Order By id) as new_SortOrder
      FROM tableName
     )
UPDATE toupdate a
    SET sortorder = new_sort_order;

SQL Server has the nice ability to update a subquery or CTE under some circumstances. 在某些情况下,SQL Server具有更新子查询或CTE的能力。

Do you need to persist the order of lists containing items that are shared between lists? 您是否需要保持包含列表之间共享项的列表顺序? If so, perhaps variations on this schema would work for you. 如果是这样,这个模式的变体可能适合您。

Item
id    label
1     A
2     B
3     C
4     D

List
id    listName
1     abc list
2     cbd list
3     aaa list

ListMembership
id    listId   itemId   order
1     1        1        1
2     1        2        2
3     1        3        3
4     2        2        2
5     2        3        1
6     2        4        3
7     3        1        1
8     3        1        2
9     3        1        3

usage: 用法:

select i.label from listMembership as lm
   join Item as i on i.id=lm.itemId
   where lm.listId=2
   order by lm.order

yields: 收益率:

label
C
B
D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle - 有序列 ID 例如 ORDER BY 1 被允许在哪里? - Oracle - where the orderly column ID e.g. ORDER BY 1 is allwed? 有没有一种方法可以使用可以将多个列作为输入的函数(例如`GREATEST`)而无需输入每个列名? - Is there a way to use functions that can take multiple columns as input (e.g. `GREATEST`) without typing out every column name? mySQL-根据另一列的alpha自动填充sortorder列 - mySQL - autopopulate a sortorder column based on alpha of another column 什么是用于临时/旋转存储的最佳/最快MySQL表架构,例如用于会话管理? - What is the best/fastest MySQL Table Schema for temporary/rotating storage, e.g. for session management? SQL搜索查询多个Where子句,例如,名字和姓氏 - SQL Search Query for Multiple Where Clauses e.g. Firstname AND Surname 查询和分组多个列属性,例如具有许多作者的书籍 - Querying and grouping multiple column attributes e.g. Books with many authors SQL的扩展占位符,例如WHERE id IN(??) - Extended placeholders for SQL, e.g. WHERE id IN (??) 是否可以根据另一列值的字母顺序将 sortOrder 值归因于一列? - Is it possible to attribute a sortOrder value to a column based on the alphabetical order of the values of another column? 将用户添加到数据库中的多个组的最佳方法是什么? - What is the best way to add users to multiple groups in a database? SELECT 的最佳方法是基于另一列的计数 - Best way to SELECT one column based on the count of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM