-1

Goal:
To retrieve the order in a right order.

Requested result in the table

a = nvarchar
n = int
text = nvarchar


a  n  text
----------
a  3  sd  
a  2  df
a  1  cv
c  2  we
c  1  qw
b  5  zx
b  4  hj
b  3  cv
b  2  fv
b  1  av
...
...
...
...
...
...
more rows to go

Problem:
I don't know how to do it with SQL code in order to retrieve the column 'n' in a right order. The right order is displayed in the goal part.

a = nvarchar
n = int
text = nvarchar


a  n  text
----------
a  1  sd  
a  2  df
a  3  cv
c  1  we
c  2  qw
b  1  zx
b  2  hj
b  3  cv
b  4  fv
b  5  av
...
...
...
...
...
...
more rows to go
4

3 回答 3

1

I: So you want to know how to update your table to take the new n column ordering into account?

You: Yes, it's correct. Changing only the column n and its chronological order for the data. The desired value start with the highest value and goes down all the way to value '1'. It can be changed or you also can add a new column.

You can update the n-column with ROW_NUMBER in a CTE:

WITH cte AS 
(
    SELECT a, n, text, 
           rn = Row_number() 
                 OVER( 
                   partition BY a 
                   ORDER BY n DESC) 
    FROM   dbo.Table1) 
UPDATE cte 
SET    n = rn; 

DEMO

于 2013-04-13T13:14:46.670 回答
0

You can specify order like that:

SELECT
    a, n, txt
FROM
    TableName
ORDER BY
    a,
    n DESC

But it will sort according to a value first too.

于 2013-04-13T12:58:39.387 回答
0
Select * from table order by a, n
于 2013-04-13T12:59:12.813 回答