SQL Server数据透视表中的数据

时间:2018-01-16 07:18:26

标签: sql sql-server pivot

我有这个查询

select 
    ed.employeeid, e.Namemployee, d.doc_code, d.doc_name, ed.received
from 
    documents_Detail ed 
inner join 
    table_emp e on e.employeeid = ed.employeeid
inner join 
    docum d on d.doc_code = ed.doc_code and ed.received = 'N'

显示数据:

employeeid  Namemployee doc_code  doc_name         received
-----------------------------------------------------------
23           John       2         Academic_Cert       N                         
23           John       6          Nom_Form           N 

和其他几个不同的doc_code和数据一样。

必需的输出:

employeeid  Nameemployee      Academic_Cert(2)   Nom_Form(6)
-------------------------------------------------------------
23            John              N                  N

2 个答案:

答案 0 :(得分:1)

两种解决方案现在都没有CTE

请尝试此操作,您可以将此动态化。

select employeeid,Namemployee,MAX([Academic_Cert(2)]) [Academic_Cert(2)] ,MAX([Nom_Form(6)]) [Nom_Form(6)]  from 
(
    SELECT * FROM 
    (
        SELECT *,CONCAT(doc_name,'(',doc_code,')') dn FROM  
        (
            select 
                ed.employeeid, e.Namemployee, d.doc_code, d.doc_name, ed.received
            from 
                documents_Detail ed 
            inner join 
                table_emp e on e.employeeid = ed.employeeid
            inner join 
                docum d on d.doc_code = ed.doc_code and ed.received = 'N'
        )k
    )u
    pivot
    (
        MAX(received) FOR dn IN ([Academic_Cert(2)],[Nom_Form(6)])
    )p
)cte1
GROUP BY employeeid,Namemployee

输出

employeeid  Namemployee Academic_Cert(2) Nom_Form(6)
----------- ----------- ---------------- -----------
23          John        N                N

(受影响的一行)

为了让它变得动态,请参阅此博客文章 https://msbiskills.com/2017/12/25/sql-puzzle-dynamic-pivot-puzzle/

另一个没有CTE的解决方案是

    select employeeid,Namemployee,MAX(CASE WHEN doc_code = 2 THEN [received] END) [Academic_Cert(2)] 
,MAX(CASE WHEN doc_code = 6 THEN [received] END) [Nom_Form(6)]  from 
(
    SELECT *,CONCAT(doc_name,'(',doc_code,')') dn FROM  
    (
        select 
            ed.employeeid, e.Namemployee, d.doc_code, d.doc_name, ed.received
        from 
            documents_Detail ed 
        inner join 
            table_emp e on e.employeeid = ed.employeeid
        inner join 
            docum d on d.doc_code = ed.doc_code and ed.received = 'N'
    )k  
)CTE
GROUP BY employeeid,Namemployee

<强>输出

employeeid  Namemployee Academic_Cert(2) Nom_Form(6)
----------- ----------- ---------------- -----------
23          John        N                N


(1 row affected)

答案 1 :(得分:0)

您可以通过执行动态SQL查询来实现此目的。为方便起见,结果集可以存储在临时表中,并在动态SQL查询中使用该临时表。

<强>查询

declare @sql as varchar(max);
select @sql = 'select [employeeid], [Namemployee], '
    + stuff((
            select ',max(case [doc_name] when ' + char(39) + [doc_name] + char(39) 
            + ' then ' + char(39) + [received] + char(39) + ' end) as [' + [doc_name] 
            + '(' + cast([doc_code] as varchar(100)) + ')]'
            from #t
            order by [doc_name]
            for xml path('')
        )
        , 1, 1, ''
    );

select @sql += ' from #t group by [employeeid], [Namemployee];';

exec(@sql);