2014-06-05 110 views
-1

我有表Employees,Documents,Transactions,from_to表。如何在SQL中將水平數據顯示爲垂直列?

員工可以將文件發送給其他員工並將該文件的副本發送給其他員工。

data will be displayed in transaction table as follow: 

TransId - Document Id - EmployeeId - from_toId 

1    1    5   1 (From) 
2    1    6   2 (To) 
3    1    10   2 (CC) 

現在;我想要上面的數據顯示如下:

DocId   From   To   CC 

1    Jo(5)  Fo(6)  Do(10) 

我知道我們需要與「數據透視表」有關的事情。但我不知道如何。

等待您的反饋。

+0

請格式化您的數據...它不會對齊。 – JonH

回答

0

我認爲這反映了你的桌子,雖然我使用的名稱,而不是ID的最後兩列:

CREATE TABLE [dbo].[Transaction](
      [TransId] [int] NOT NULL, 
      [DocId] [int] NOT NULL, 
      [EmpId] [nvarchar](10) NOT NULL, 
      [FromToId] [nchar](10) NOT NULL 
) ON [PRIMARY] 

INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(1,1,'Jo','From') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(2,1,'Fo','To') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(3,1,'Do','CC') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(4,2,'Jo','From') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(5,2,'Bo','To') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(6,2,'Zo','CC') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(7,3,'Bo','From') 
INSERT INTO [Transaction] ([TransId],[DocId],[EmpId],[FromToId])VALUES(8,3,'Go','To') 

那麼這個查詢會給你你所要求的結果:

SELECT DISTINCT 
     t.DocId 
     ,x.[From] 
     ,y.[To] 
     ,z.Cc 
FROM [Transaction] t 
LEFT JOIN 
(
    SELECT DocId 
    ,CASE WHEN FromToId = 'From' THEN EmpId END AS [From] 
    FROM [Transaction] 
    WHERE CASE WHEN FromToId = 'From' THEN EmpId END IS NOT NULL 
) x ON t.DocId = x.DocId 
LEFT JOIN 
(
    SELECT DocId 
    ,CASE WHEN FromToId = 'To' THEN EmpId END AS [To] 
    FROM [Transaction] 
    WHERE CASE WHEN FromToId = 'To' THEN EmpId END IS NOT NULL 
) y ON t.DocId = y.DocId 
LEFT JOIN 
(
    SELECT DocId 
    ,CASE WHEN FromToId = 'CC' THEN EmpId END AS [Cc] 
    FROM [Transaction] 
    WHERE CASE WHEN FromToId = 'Cc' THEN EmpId END IS NOT NULL 
) z ON t.DocId = z.DocId 

DocId From To Cc 
    1  Jo Fo Do 
    2  Jo Bo Zo 
    3  Bo Go NULL 
1

我有一個動態光標的解決方案:

CREATE TABLE #PRERESULT(
[TransId] int, 
DocumentID int , 
EmployeeId int , 
from_toId [nvarchar](10) 
) 

INSERT INTO #PRERESULT ([TransId],DocumentID,EmployeeId,from_toId) 
VALUES 
(1,1,5,'1 (From)'), 
(2,1,6,'2 (To)'), 
(3,1,10,'2 (CC)') 

CREATE TABLE #RESULT (
DocID int, 
[From] nvarchar(15), 
[To] nvarchar(15), 
CC nvarchar(15)) 

INSERT INTO #RESULT (DocID) 
SELECT DocumentID 
FROM #PRERESULT 
GROUP BY DocumentID 

DECLARE @Documentid int,@Employee int, @Alias nvarchar(10),@SQL nvarchar(250) 

DECLARE C_FromTo CURSOR 
FOR 
    SELECT DocumentID,EmployeeID 
    FROM #PRERESULT 

OPEN C_FromTo 


FETCH NEXT FROM C_FromTo INTO @Documentid, @Employee 
While (@@Fetch_status = 0) 
BEGIN 

SET @Alias = (SELECT SUBSTRING(from_toId,PATINDEX('%(%',from_toId)+1,(LEN(from_toId)-PATINDEX('%(%',from_toId)-1)) FROM #PRERESULT WHERE @Employee = EmployeeId) 

SET @SQL = 'UPDATE #RESULT 
SET ['[email protected]+'] = '+Convert(nvarchar(50),@Employee)+' 
WHERE '+Convert(nvarchar(50),@Documentid)+' = DocID' 

EXEC (@SQL) 

FETCH NEXT FROM C_FromTo INTO @Documentid, @Employee 
END 
CLOSE C_FromTo 
DEALLOCATE C_FromTO 

SELECT * FROM #RESULT 


DROP TABLE #PRERESULT 
DROP TABLE #RESULT 

給你這樣的:

的DocID |從|要| CC
            | 5             | 6     | 10

希望這將幫助你
哈祝你有個愉快的日子&來自瑞士的Greets

Etienne

相關問題