我有以下TypeStatusDetails
表。此表有不同的TypeID
s。每種類型可以具有最多4個(或更少),即例如對於僅有3個StatusID
s)。在sql中使用PIVOT
RecordCount顯示no。的特定狀態ID的記錄。 (如3個掛起記錄TYPE_1)
---------------------------------------------------------------
TypeID Type StatusID StatusName RecordCount
---------------------------------------------------------------
1 Type_1 1 Pending 3
1 Type_1 2 In Process 2
1 Type_1 3 Completed 1
1 Type_1 4 Invalid 1
2 Type_2 1 Pending 4
2 Type_2 2 In Process 5
2 Type_2 3 Completed 6
2 Type_2 4 Invalid 1
3 Type_3 1 Pending 1
3 Type_3 2 In Process 1
3 Type_3 3 Completed 1
我想結果如下表所示:
根據每種類型,我想顯示的RecordCount按照與StatusID狀態。
--------------------------------------------------------------------------------
Type Type Status Pending Status InProcess Status Completed Status Invalid ID ID Count ID Count ID Count ID Count
for for for for
Pending InProcess Completed Invalid
--------------------------------------------------------------------------------
1 Type_1 1 3 2 2 3 1 4 1
2 Type_2 1 4 2 5 3 6 4 1
3 Type_3 1 1 2 1 3 1 4 0
我用下面的查詢
SELECT *
FROM (
SELECT
TypeID,Type,StatusName,RecordCount
FROM #TypeStatusDetails
) as s
PIVOT
(
SUM(RecordCount)
FOR StatusName IN ([Pending],[In Process],[Completed],[Invalid])
)AS pvt
但我無法得到StatusId列按每個StatusName它樞軸結合起來。
任何人都可以幫助我嗎?
你RDMBS是SQL Server的我承擔? –