例如,我需要從如何在sql server 2005/8中創建沒有聚合函數的Column to Row?
改變
。
我知道PIVOT就是爲了這個,但它需要一個聚合函數;對於我的情況,我不需要聚合只需要列到行。如果有將是一個創紀錄的主題,你可以使用MIN或MAX
CREATE TABLE[StudentScores]
(
[UserName] NVARCHAR(20),
[Subject] NVARCHAR(30),
[Score]FLOAT,
)
GO
INSERT INTO[StudentScores]SELECT'Nick','Chinese',80
INSERT INTO[StudentScores]SELECT'Nick','Maths',90
INSERT INTO[StudentScores]SELECT'Nick','English',70
INSERT INTO[StudentScores]SELECT'Nick','Biology',85
INSERT INTO[StudentScores]SELECT'Kent','Chinese',80
INSERT INTO[StudentScores]SELECT'Kent','Maths',90
INSERT INTO[StudentScores]SELECT'Kent','English',70
INSERT INTO[StudentScores]SELECT'Kent','Biology',85
所以你實際上想要多列名爲'80','90','70','85'?或者你是否在爲每個用戶名尋找一行,每一列都是主題?在這種情況下,@OMG小馬的答案可以滿足你的需求,而且你可以看到你確實需要一個聚合。 – 2010-10-26 04:35:40