以顺时针方向旋转表格中的列值

时间:2016-03-21 11:14:32

标签: sql-server

I have a table like this 
   c1   c2  c3
    1   2   3
    4   5   6
    7   8   9

I need output like this......       
    c1  c2  c3
    9   1   2
    3   4   5
    6   7   8

我试过但我没有任何想法 如果有人知道答案,请为该答案添加一些解释.........

1 个答案:

答案 0 :(得分:3)

请尝试,这可能有助于您解决问题

DECLARE @V_MAX INT 
SELECT  @V_MAX = c3 FROM [tablec]
;WITH CTE_TEST
AS
(   SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS ROW_NO,*   FROM [tablec] 
)
SELECT ISNULL(T.c3,@V_MAX) C1,M.c1 C2,M.c2 C3 FROM CTE_TEST M
LEFT OUTER JOIN (SELECT * FROM CTE_TEST T ) T ON T.ROW_NO = M.ROW_NO-1