如何按列和下一行排序并按另一列排序?

How to sort by column and next row and order them by another column?

我有一个包含四列的 table:IDisErrorSolidLineHighestError

每行通过 SolidLine 列与另一行相关。所以我们在 table.

中有两个相关的行

例如,具有 ID 1 和 2 的行具有 SolidLine(5) 的关系。

----------------------------------------------------------------------
|      ID      |    isError     |      SolidLine   |    HighestError
----------------------------------------------------------------------
|       1      |     0          |        5         |     1
|       2      |     0          |        5         |     1
|       3      |     0          |        8         |     1
|       4      |     0          |        8         |     1
|       5      |     1          |        10        |     50 
|       6      |     0          |        10        |     1
|       7      |     1          |        4         |     80
|       8      |     0          |        4         |     1
|       9      |     1          |        7         |     80
|      10      |     0          |        7         |     1
|      11      |     0          |        3         |     1 
|      12      |     0          |        3         |     1
----------------------------------------------------------------------

我想按以下条件对 table 进行排序:

If isError is 1, take the next row by SolidLine, then order by HighestError

所以希望的结果应该是这样的:

----------------------------------------------------------------------
|      ID      |    isError     |      SolidLine   |    HighestError
----------------------------------------------------------------------
|       7      |     1          |        4         |   80
|       8      |     0          |        4         |   1
|       9      |     1          |        7         |   80
|       10     |     0          |        7         |   1
|       5      |     1          |        10        |   50
|       6      |     0          |        10        |   1
|       1      |     0          |        5         |   1
|       2      |     0          |        5         |   1
|       3      |     0          |        8         |   1
|       4      |     0          |        8         |   1
|       11     |     0          |        3         |   1
|       12     |     0          |        3         |   1 
----------------------------------------------------------------------

第一行成为第一行,因为 HighestError 在 table isError 中的最大值等于 1。然后下一行是 ID = 8 因为它 SolidLine 具有与 ID = 7.

相同的行值 SolidLine

SolidLine 总是在一起,不依赖于 isError 列。

所以 SolidLine 并列的那对行应该总是在一起。

我尝试了以下查询,但给出了错误的结果:

--it breaks SolidLine ordering. 
SELECT ID, isError, SolidLine, HighestError
FROM SolidThreads
ORDER BY SolidLine, isError, HighestError desc, id

和:

SELECT 
ROW_NUMBER() OVER (PARTITION BY SolidLine ORDER BY isError DESC) [RowNumber],
ID, isError, SolidLine, HighestError
FROM SolidThreads
ORDER BY HighestError desc, id

我做错了什么?或者我该怎么做?

您似乎想要将 SolidLines 保持在一起进行排序,并首先按 HighestError 对这些组进行排序,然后按组中最低的 ID 排序,然后在组内首先显示错误。假设这就是你想要的,我会用派生的 table:

ID, isError, SolidLine, HighestError
FROM SolidThreads INNER JOIN
(SELECT SolidLine, MAX(Highesterror) as sorting_HighestError, MIN(ID) as Sorting_Id
    FROM SolidThreads GROUP BY SolidLine) as Sorting_DT
ON Sorting_DT.SolidLine = SolidThreads.SolidLine
ORDER BY sorting_HighestError DESC, Sorting_Id, isError Desc, Id

正如您所描述的那样,您应该能够通过...

  • 为 "This Solid Line Includes an Error Row"
  • 添加一列
  • 为 "The max error for this Solid Line"
  • 添加一列
  • 使用 CASE 表达式更改基于错误状态的排序

http://sqlfiddle.com/#!18/84e7a/1

WITH
  SolidThreadsSummary AS
(
  SELECT
    *,
    MAX(isError     ) OVER (PARTITION BY SolidLine)   AS SolidLineHasError,
    MAX(highestError) OVER (PARTITION BY SolidLine)   AS SolidLineMaxError
  FROM
    SolidThreads
)
SELECT
  *
FROM
  SolidThreadsSummary
ORDER BY
  SolidLineHasError DESC,  -- Not really necessary for your data
  SolidLineMaxError DESC,
  CASE WHEN SolidLineHasError > 0 THEN SolidLine ELSE 1 END,
  isError DESC,
  id


如果对不总是按 id 连续(对于不包含错误的对),这可能会更稳健一些...

http://sqlfiddle.com/#!18/84e7a/2

WITH
  SolidThreadsSummary AS
(
  SELECT
    *,
    MAX(isError     ) OVER (PARTITION BY SolidLine)   AS SolidLineHasError,
    MAX(highestError) OVER (PARTITION BY SolidLine)   AS SolidLineMaxError,
    MIN(id          ) OVER (PARTITION BY SolidLine)   AS SolidLineMinID
  FROM
    SolidThreads
)
SELECT
  *
FROM
  SolidThreadsSummary
ORDER BY
  SolidLineHasError DESC,
  SolidLineMaxError DESC,
  CASE WHEN SolidLineHasError > 0 THEN SolidLine ELSE 1 END,
  isError DESC,
  SolidLineMinID,
  id
;

如果每个 SolidLine 对的 ID 始终是连续的,您可以简单地执行 this:

SELECT T.*
FROM yourTable T
JOIN (SELECT SolidLine, MAX(HighestError) MaxError
      FROM yourTable
      GROUP BY SolidLine) T2 ON T.SolidLine = T2.SolidLine
ORDER BY MaxError DESC, ID