(ROW_NUMBER)ORDER BY,而不是索引正确的列

时间:2014-01-02 15:06:44

标签: sql sql-server

我得到了这样的查询:

SELECT COL_A, COL_B, row FROM (
    SELECT (SELECT COUNT(*) AS Expr1 
                FROM ObjectEventEffect
                WHERE      (ObjectEventId = ObjectEvent.Id) AND (Status = 'E')) AS COLA_A,
                COL_B, 
                ROW_NUMBER() OVER (ORDER BY COL_B ASC) ROW
                FROM         ObjectEvent
) a WHERE a.ROW >= 0 AND a.ROW <= 10

返回:

COLA_A  COL_B           ROW
1   2013-10-14 19:51:40.757 4
0   2013-10-16 09:53:46.043 8
0   2013-10-14 19:51:38.443 1
0   2013-10-14 19:51:41.007 5
1   2013-10-16 09:53:46.310 9
0   2013-10-14 19:51:40.053 2
0   2013-10-16 09:53:44.667 6
0   2013-10-16 09:53:46.560 10
0   2013-10-14 19:51:40.413 3
0   2013-10-16 09:53:45.793 7

我想要的结果可以通过删除COL_A

的SELECT来实现
COL_B                   ROW
2013-10-14 19:51:38.443 1
2013-10-14 19:51:40.053 2
2013-10-14 19:51:40.413 3
2013-10-14 19:51:40.757 4
2013-10-14 19:51:41.007 5
2013-10-16 09:53:44.667 6
2013-10-16 09:53:45.793 7
2013-10-16 09:53:46.043 8
2013-10-16 09:53:46.310 9
2013-10-16 09:53:46.560 10

有没有办法在不破坏COL_B顺序的情况下显示COL_A和COL_B?

1 个答案:

答案 0 :(得分:1)

尝试

SELECT COL_A,
       COL_B,
       COL_C,
       ROW_NUMBER() OVER(ORDER BY COL_C) AS [ROW]
FROM A_TABLE
order by [ROW] asc