1
請參閱下面的代碼片段:樞軸在SQL Server中柱串聯
DROP TABLE IF EXISTS PROJECT_DETAILS;
CREATE TABLE PROJECT_DETAILS
(
PROJECT_NAME NVARCHAR(20),
PROJECT_TYPE NVARCHAR(20),
TOTAL_HOURS INT
);
INSERT INTO PROJECT_DETAILS VALUES('ProjectA','AU',100)
INSERT INTO PROJECT_DETAILS VALUES('ProjectA','SGP',50)
INSERT INTO PROJECT_DETAILS VALUES('ProjectA','NZ',75)
INSERT INTO PROJECT_DETAILS VALUES('ProjectB','US',200)
INSERT INTO PROJECT_DETAILS VALUES('ProjectB','CAN',100)
INSERT INTO PROJECT_DETAILS VALUES('ProjectC','JP',120)
INSERT INTO PROJECT_DETAILS VALUES('ProjectD','IND',100)
INSERT INTO PROJECT_DETAILS VALUES('ProjectD','CH',80)
INSERT INTO PROJECT_DETAILS VALUES('ProjectE','RSA',90)
INSERT INTO PROJECT_DETAILS VALUES('ProjectE','KEN',30)
所需的輸出
PROEJCT_NAME PROJECT_TYPE EXEC_TYPE TOTAL_HRS
ProjectA AU AU-SGP-NZ 100
ProjectA SGP AU-SGP-NZ 50
ProjectA NZ AU-SGP-NZ 75
ProjectB US US-CAN 200
ProjectB CAN US-CAN 100
ProjectC JP JP 120
ProjectD IND IND-CH 100
ProjectD CH IND-CH 80
ProjectE RSA RSA-KEN 90
ProjectE KEN RSA-KEN 30
正如你可以在上面看到,我想有串聯的列名每個不同的項目名稱(作爲新列)。如果您需要任何其他詳細信息,請告訴我。
如何在EXEC_type定義PROJECT_TYPE訂單? – qxg
嗨,它出現的順序並不重要。也許,現在,您可以通過TOTAL_HOURS排序 –
[將多行連接成單個文本字符串?](https://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-文本字符串) – qxg