Pivot table(或其他解决方案)与内部连接

Pivot table (or other solution) with Inner join

我有这个查询:

SELECT  DISTINCT  
    a.max_TEA_InicioTarefa, 
    analista,
    ETS.ETS_Sigla, 
    ATC.ATC_Id, 
    ATC.ATC_Sigla, 
    PAT.PAT_Sigla, 
    a.SRV_Id,
    ContratoComunicado.CCM_Id,
    ContratoComunicado.CCM_Docto, 
    ContratoComunicado.CCM_Emissao,
    ComunicadoTipo.CMT_Descr,
    TarefaEtapaAreaTecnica_1.TEA_Revisao, 
    TarefaEtapaAreaTecnica_1.ETS_Id, 
    TarefaEtapaAreaTecnica_1.TEA_FimTarefa, 
    TarefaEtapaAreaTecnica_1.PAT_Id 
FROM       
    dbo.Tarefa AS Tarefa_1
INNER JOIN
    (
        SELECT 
            MAX(dbo.TarefaEtapaAreaTecnica.TEA_InicioTarefa) 
                AS max_TEA_InicioTarefa, 
            dbo.Pessoa.PFJ_Descri as analista,
            dbo.AreaTecnica.ATC_Id, 
            dbo.Tarefa.SRV_Id
        FROM  
            dbo.TarefaEtapaAreaTecnica 
            LEFT OUTER JOIN dbo.Tarefa 
                ON dbo.TarefaEtapaAreaTecnica.TRF_Id = 
                dbo.Tarefa.TRF_Id
            LEFT OUTER JOIN dbo.AreaTecnica 
                ON dbo.TarefaEtapaAreaTecnica.ATC_Id = 
                dbo.AreaTecnica.ATC_Id 
            LEFT OUTER JOIN dbo.Pessoa 
                ON dbo.Pessoa.PFJ_Id = 
                dbo.TarefaEtapaAreaTecnica.PFJ_Id_Analista
        GROUP BY 
            dbo.AreaTecnica.ATC_Id, 
            dbo.Tarefa.SRV_Id, 
            dbo.Pessoa.PFJ_Descri
    ) AS a 
        ON Tarefa_1.SRV_Id = a.SRV_Id 
INNER JOIN dbo.TarefaEtapaAreaTecnica AS TarefaEtapaAreaTecnica_1 
    ON Tarefa_1.TRF_Id = TarefaEtapaAreaTecnica_1.TRF_Id AND 
    a.ATC_Id = TarefaEtapaAreaTecnica_1.ATC_Id AND
    a.max_TEA_InicioTarefa = TarefaEtapaAreaTecnica_1.TEA_InicioTarefa
LEFT JOIN AreaTecnica ATC ON 
    TarefaEtapaAreaTecnica_1.ATC_Id = ATC.ATC_Id
LEFT JOIN Etapa ETS ON 
    TarefaEtapaAreaTecnica_1.ETS_Id = ETS.ETS_Id
LEFT JOIN ParecerTipo PAT ON 
    TarefaEtapaAreaTecnica_1.PAT_Id = PAT.PAT_Id 
LEFT JOIN dbo.Servico ON 
    dbo.Servico.SRV_Id = Tarefa_1.SRV_Id
LEFT JOIN dbo.Contrato ON 
    dbo.Contrato.CNT_Id = Servico.CNT_Id
LEFT JOIN dbo.ContratoComunicado ON 
    dbo.Contrato.CNT_Id = ContratoComunicado.CNT_Id 
LEFT JOIN dbo.ComunicadoTipo ON 
    dbo.ComunicadoTipo.CMT_Id = ContratoComunicado.CMT_Id  

结果我有以下几行:

[1]: http://i.stack.imgur.com/2btx9.jpg

例如。第 6 行和第 7 行几乎相同,但是 CCM_Id、CCM_Docto、CCM_Emissao(来自 table ContratoComunicado)和 CMT_Descr(来自 table ComunicadoTipo) 不是,我需要将 CMT_Descr(来自 table ComunicadoTipo)作为每个 CMT_Id 的列放在 table ContratoComunicado 中,所以我将有:

广告前日期 | Pre-Advertencia Docto | 1ª 广告日期等。
2014-05-08 | 46/00365/14 | 2014-05-10

通过这样做,我将保证不会有相等的行。 我在这方面遇到了麻烦,有人可以帮助我吗?谢谢

完成,我设法用老方法来做:

 SELECT DISTINCT   
    SRV.INT_Id, DOCTO.CNT_Id,
     a.max_TEA_InicioTarefa, 
           analista,
           ETS.ETS_Sigla, 
           ATC.ATC_Id, 
           ATC.ATC_Sigla, 
           PAT.PAT_Sigla, 
           a.SRV_Id,
           TarefaEtapaAreaTecnica_1.TEA_Revisao, 
           TarefaEtapaAreaTecnica_1.ETS_Id, 
           TarefaEtapaAreaTecnica_1.TEA_FimTarefa, 
           TarefaEtapaAreaTecnica_1.PAT_Id, 
           DATAPRE, 
           DOCTOPRE, 
           DATA1A,
           DOCTO1A,
           DATA2A,
           DOCTO2A, 
           DATA3A,
           DOCTO3A, 
           DATAPA, 
           DOCTOPA, 
           DATARESC, 
           DOCTORESC
FROM       
    dbo.Tarefa AS Tarefa_1
INNER JOIN
    (
        SELECT 
            MAX(dbo.TarefaEtapaAreaTecnica.TEA_InicioTarefa) 
                AS max_TEA_InicioTarefa, 
            dbo.Pessoa.PFJ_Descri as analista,
            dbo.AreaTecnica.ATC_Id, 
            dbo.Tarefa.SRV_Id
        FROM  
            dbo.TarefaEtapaAreaTecnica 
            LEFT OUTER JOIN dbo.Tarefa 
                ON dbo.TarefaEtapaAreaTecnica.TRF_Id = 
                dbo.Tarefa.TRF_Id
            LEFT OUTER JOIN dbo.AreaTecnica 
                ON dbo.TarefaEtapaAreaTecnica.ATC_Id = 
                dbo.AreaTecnica.ATC_Id 
            LEFT OUTER JOIN dbo.Pessoa 
                ON dbo.Pessoa.PFJ_Id = 
                dbo.TarefaEtapaAreaTecnica.PFJ_Id_Analista
        GROUP BY 
            dbo.AreaTecnica.ATC_Id, 
            dbo.Tarefa.SRV_Id, 
            dbo.Pessoa.PFJ_Descri
    ) AS a 
        ON Tarefa_1.SRV_Id = a.SRV_Id 
INNER JOIN dbo.TarefaEtapaAreaTecnica AS TarefaEtapaAreaTecnica_1 
    ON Tarefa_1.TRF_Id = TarefaEtapaAreaTecnica_1.TRF_Id AND 
    a.ATC_Id = TarefaEtapaAreaTecnica_1.ATC_Id AND
    a.max_TEA_InicioTarefa = TarefaEtapaAreaTecnica_1.TEA_InicioTarefa
LEFT JOIN AreaTecnica ATC ON 
    TarefaEtapaAreaTecnica_1.ATC_Id = ATC.ATC_Id
LEFT JOIN Etapa ETS ON 
    TarefaEtapaAreaTecnica_1.ETS_Id = ETS.ETS_Id
LEFT JOIN ParecerTipo PAT ON 
    TarefaEtapaAreaTecnica_1.PAT_Id = PAT.PAT_Id 
LEFT JOIN dbo.Servico ON 
    dbo.Servico.SRV_Id = Tarefa_1.SRV_Id
LEFT JOIN dbo.Contrato ON 
    dbo.Contrato.CNT_Id = Servico.CNT_Id
LEFT JOIN dbo.ContratoComunicado ON 
    dbo.Contrato.CNT_Id = ContratoComunicado.CNT_Id 
LEFT JOIN dbo.ComunicadoTipo ON 
    dbo.ComunicadoTipo.CMT_Id = ContratoComunicado.CMT_Id  
LEFT JOIN Servico SRV ON 
    SRV.SRV_Id = Tarefa_1.SRV_Id
inner JOIN 
(
SELECT CC.CNT_Id,
         DATAPRE = MAX(case when CC.CMT_Id=5 then CC.CCM_Emissao end)
         , DOCTOPRE = MAX(case when CC.CMT_Id=5 then CC.CCM_Docto end)
         , DATA1A = MAX(case when CC.CMT_Id=2 then CC.CCM_Emissao end)
         , DOCTO1A = MAX(case when CC.CMT_Id=2 then CC.CCM_Docto end)
         , DATA2A = MAX(case when CC.CMT_Id=3 then CC.CCM_Emissao end)
         , DOCTO2A = MAX(case when CC.CMT_Id=3 then CC.CCM_Docto end)
         , DATA3A = MAX(case when CC.CMT_Id=7 then CC.CCM_Emissao end)
         , DOCTO3A = MAX(case when CC.CMT_Id=7 then CC.CCM_Docto end)
         , DATAPA = MAX(case when CC.CMT_Id=8 then CC.CCM_Emissao end)
         , DOCTOPA = MAX(case when CC.CMT_Id=8 then CC.CCM_Docto end)
         , DATARESC = MAX(case when CC.CMT_Id=4 then CC.CCM_Emissao end)
         , DOCTORESC = MAX(case when CC.CMT_Id=4 then CC.CCM_Docto end)
from ContratoComunicado AS CC  
GROUP BY CC.CNT_Id 
) AS DOCTO
ON DOCTO.CNT_Id = SRV.CNT_Id