Sql server 如何按两种方式对一列进行排序

Sql server 如何按两种方式对一列进行排序,sql-server,sql-order-by,sql-insert,sql-server-2016,temp-tables,Sql Server,Sql Order By,Sql Insert,Sql Server 2016,Temp Tables,我有一个名为product的表及其数据示例data structer,如下所示 +-----------+-------------+-----------+ | ProductID | ProductName | SortValue | +-----------+-------------+-----------+ | 1 | AA12 | 0 | +-----------+-------------+-----------+ | 2

我有一个名为product的表及其数据示例data structer,如下所示

+-----------+-------------+-----------+
| ProductID | ProductName | SortValue |
+-----------+-------------+-----------+
| 1         | AA12        | 0         |
+-----------+-------------+-----------+
| 2         | AA10        | 0         |
+-----------+-------------+-----------+
| 3         | AA11        | 0         |
+-----------+-------------+-----------+
| 4         | AA13        | 0         |
+-----------+-------------+-----------+
| 5         | AA1         | 2         |
+-----------+-------------+-----------+
| 6         | AA2         | 1         |
+-----------+-------------+-----------+
| 7         | AA3         | 4         |
+-----------+-------------+-----------+
| 8         | AA4         | 3         |
+-----------+-------------+-----------+
| 9         | AA5         | 5         |
+-----------+-------------+-----------+
| 10        | AA6         | 6         |
+-----------+-------------+-----------+
我需要将此表数据插入另一个临时表,并使用其
SortValue
进行排序。在这里,您可以看到多个
sortvalue
0
。那些由
0
组成的
SortValue
应该通过其产品名称插入表的末尾。我的预期产出应该是

+-----------+-------------+-----------+
| ProductID | ProductName | SortValue |
+-----------+-------------+-----------+
| 6         | AA2         | 1         |
+-----------+-------------+-----------+
| 5         | AA1         | 2         |
+-----------+-------------+-----------+
| 8         | AA4         | 3         |
+-----------+-------------+-----------+
| 7         | AA3         | 4         |
+-----------+-------------+-----------+
| 9         | AA5         | 5         |
+-----------+-------------+-----------+
| 10        | AA6         | 6         |
+-----------+-------------+-----------+
| 2         | AA10        | 0         |
+-----------+-------------+-----------+
| 3         | AA11        | 0         |
+-----------+-------------+-----------+
| 1         | AA12        | 0         |
+-----------+-------------+-----------+
| 4         | AA13        | 0         |
+-----------+-------------+-----------+
我该怎么做?我刚试过这样的东西

DECLARE @tmp TABLE (
    [ProductID] INT,
    [ProductName] VARCHAR(50),
    [SortValue] INT )
    
    INSERT INTO @tmp
    p.ProductID,p.ProductName,p.SortValue
    SELECT FROm Product p ORDER by p.SortValue

首先:数据库表表示无序的行集。行没有固有的顺序:当您从表中选择
时,您可以通过
order by
子句控制返回行的顺序

因此,我理解您的问题是如何按相关顺序选择行。为此,可以使用条件排序:

select t.*
from mytbable
order by 
    case when sortValue = 0 then 1 else 0 end,
    sortValue,
    productName

第一级排序中的
case
表达式将
sortValue
不是
0
的行放在第一位。然后,组按
sortValue
productName

进行排序,作为起点:数据库表表示无序的行集。行没有固有的顺序:当您从表中选择
时,您可以通过
order by
子句控制返回行的顺序

因此,我理解您的问题是如何按相关顺序选择行。为此,可以使用条件排序:

select t.*
from mytbable
order by 
    case when sortValue = 0 then 1 else 0 end,
    sortValue,
    productName

第一级排序中的
case
表达式将
sortValue
不是
0
的行放在第一位。然后,分组按
sortValue
productName

排序非常感谢您的回答非常感谢您的回答