在Sql Server中更改列的顺序 [英] Change Sequence of Column in Sql Server
问题描述
HI Everyone,
我正在使用sql的pivot功能。它工作正常。
但是我想改变创建枢轴的列序列
以下示例:
表A:
月|金额|性别|姓名
jan 20男A
2月30日女B
3月40日男C
对于此表我使用以下查询
SELECT * FROM (
选择月,金额,名称从 A
) AS S
PIVOT(
SUM(金额)
FOR 名称 IN ([A],[B],[C])) AS p
这会给我输出
月|性别| A | B | C
JAN男20 Null Null
2月女性无效30 Null
3月男性空无效40
但是我想要列的序列如
月,A,B,C然后性别
这意味着由枢轴产生的列应该位于第二个并且在剩余的列之后
注意:pivot生成动态列,这就是我不知道列名的原因
例如我将在这个例子中将a,b,c作为列名。
Thanx
Chetan V.
你可以参考这个
如何更改-in-table-in-sql-server-2008中的列顺序 [ ^ ]
让我知道是否是有用..
HI Everyone,
I am using pivot function of sql. its working fine.
but i want to change the column sequence which pivot created
Following are example :
Table A :
Month | Amount | Gender | Name
jan 20 Male A
Feb 30 Female B
March 40 Male C
For this table i am using following query
SELECT * FROM (
Select Month,Amount,Name From A
)AS S
PIVOT(
SUM(Amount)
FOR Name IN ([A],[B],[C]))AS p
this will give me output
Month |Gender| A |B |C
JAN Male 20 Null Null
Feb Female Null 30 Null
March Male Null Null 40
But I want The sequence of column like
Month,A,B,C then Gender
That mean column generated by pivot should come second and after that remaining column
NOTE : pivot generated Dynamically column that's why i am not aware about the column name
for example i will put a,b,c as column name in this example.
Thanx
Chetan V.
You can refer this
how-to-change-the-column-order-of-an-existing-table-in-sql-server-2008[^]
Let me know if it is useful..
这篇关于在Sql Server中更改列的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!