2012-12-21 3 views
0

의 해당 값을 추가하려면 : 나는 싶습니다 피벗에서 합계 (idIndice)를 사용하는 대신변경 PIVOT 기능이 쿼리에서 대신 SUM

id 10  11  12 13  9 
13 NULL 11  12 NULL NULL 
14 NULL 11  12 NULL NULL 
16 NULL NULL 12 NULL NULL 

:

DECLARE @cols VARCHAR(1000) 
DECLARE @sqlquery VARCHAR(2000) 

SELECT @cols = STUFF((SELECT distinct ',' + QuoteName(idIndice) 
         FROM tgpwebged.dbo.sistema_Indexacao as a FOR XML PATH('') 
         ), 1, 1, '')  

SET @sqlquery = 'SELECT * FROM 
     (  
     select distinct a.id, b.idIndice 
     from tgpwebged.dbo.sistema_Documentos as a 
     join tgpwebged.dbo.sistema_Indexacao as b on a.id = b.idDocumento 
     join tgpwebged.dbo.sistema_DocType as c on a.idDocType = c.id 
     join tgpwebged.dbo.sistema_DocType_Index as d on c.id = d.docTypeId 
     where d.docTypeId = 40  
     and (b.idIndice = 11 AND b.valor = ''11111111'' OR b.idIndice = 12 AND b.valor = ''11111'')  
     ) base 
     PIVOT 
     (
     Sum(idIndice) 
     FOR [idIndice] IN (' + @cols + ') 
     ) AS finalpivot' 

EXECUTE (@sqlquery) 

나는이 결과를 얻을 idIndex에 해당하는 값을 삽입하는 방법을 찾으십시오.

이러한 값은 a.value에서 sabe 테이블에 저장됩니다. 이것을 수행하는 쉬운 방법입니까? 이를 발견 할수 없어 아무데도

답변

0

각 idIndice에 대해 하나의 a.value이있는 경우, 당신은 단순히 MIN (a.value) 또는 MAX (a.value) 대신 SUM (idIndice)를 사용할 수 있습니다