如何在SQL中将水平数据显示为垂直列?

时间:2014-06-05 21:52:32

标签: sql-server

我有表Employees,Documents,Transactions,from_to table。

员工可以将文档发送给其他员工,并将该文档的副本发送给其他员工。

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)

我知道我们需要与“数据透视表”有关。但我不知道如何。

等待您的反馈。

2 个答案:

答案 0 :(得分:1)

我有一个动态的Cursor解决方案:

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 ['+@Alias+'] = '+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
1 | 5 | 6 | 10

希望这会对你有所帮助 祝你有愉快的一天。来自瑞士的问候

艾蒂安

答案 1 :(得分: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