订购sql server order by子句的问题

时间:2017-02-15 13:14:03

标签: sql sql-server

Image

检查图像

我的sql server订单有问题,我需要在Ejey字段中对记录进行排序,如下所示:

  1. Alta / Deficiente
  2. Baja / Optima
  3. Deficiente / Deficiente
  4. 媒体/阿尔塔
  5. 的Optima / Deficiente
  6. ....所以很便宜

    如所选的灰色记录所示,我澄清我的图像表有625条记录,其他字段必须保持不变。

1 个答案:

答案 0 :(得分:0)

只要EjeY的每个值具有相同的行数(5),一个想法是按行计数的行数的模数对行进行排序。 这可能有效:

--Save your data in a temp table ordered by the value of EjeY column
SELECT *
INTO #TEMP2 
FROM 
(
SELECT *
FROM YouTable
) AS TEMP1
ORDER BY TEMP1.EjeY --Here you modify the ordering mode you want every time your EjeY values to be displayed

--For each row of the temp table calculate the module the row number 
--with the count of rows per EjeY value 
--(that must be 5 in your situation)
--and order the table by this value and then by row number)

SELECT 
row_col%(select top 1 count(EjeY) from YouTable group by EjeY) AS mod_col, 
row_col,
TEMP3.* --your columns
FROM 
(SELECT *,
ROW_NUMBER() OVER (ORDER BY (SELECT null))-1 as row_col
FROM #TEMP2) as TEMP3 
ORDER BY mod_col,TEMP3.row_col