請參閱下面的DDL:SQL語句來生成列的值
create table Person (ID int, [Type] int)
insert into Person values (1,1)
insert into Person values (2,1)
insert into Person values (3,2)
insert into Person values (4,3)
insert into Person values (5,4)
insert into Person values (6,5)
我在尋找這樣一個結果:
2 1 1 1 1
以下標準產生這樣的結果:
There are 2 persons with a type of 1 (The first column value is: 2)
There is 1 person with a type of 2 (The second column value is: 1)
There is 1 person with a type of 3 (The third column value is: 1)
There is 1 person with a type of 4 (The forth column value is: 1)
There is 1 person with a type of 5 (The fifth column value is: 1)
I T嗨,你問的是一個PIVOT查詢。 – Randy
類型限制爲5個選項還是n?如果n然後http://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query如果有限,那麼你不必使用動態SQL和下面的情況下工作或標準的樞紐。 – xQbert