2017-07-15 1 views
1

je la requête suivante:valeurs de l'enregistrement Pivoting aux noms de colonnes avec les tables jointes

SELECT 
    sp.SAMPLE_ID, 
    sp.PA_NAME, 
    sp.NRESULT, 
    sp.PARAM_UNITS, 
    s.LOT 
FROM 
    DANM..SAMPLEPARAM AS sp 
    LEFT JOIN DANM..SAMPLE AS s 
     ON sp.SAMPLE_ID = s.SAMPLE_ID 
WHERE 
    s.METHOD_SUBCLASS = 'BATCH' 
    AND sp.PA_NAME IN ('AL_V','CA_V','CO_V') 
    AND sp.ENTERED_ON > DATEADD(MONTH,-12,GETDATE()) 
ORDER BY 
    s.LOT, sp.PA_NAME 

Un sous-ensemble des données résultant ressemble à quelque chose comme ceci:

SAMPLE_ID | PA_NAME | NRESULT | PARAM_UNITS | LOT 
010001459826 | AL_V | 1.70000000 | PPB   | U6X599 
010001459826 | CA_V | 1.40000000 | PPB   | U6X599 
010001459826 | CO_V | 0.70000000 | PPB   | U6X599 
010001459828 | AL_V | 2.40000000 | PPB   | U6X600 
010001459828 | CA_V | 1.60000000 | PPB   | U6X600 
010001459828 | CO_V | 0.70000000 | PPB   | U6X600 

Je suis en train de faire pivoter la enregistrements de PA_NAME tels que j'obtiens un enregistrement unique pour chaque valeur LOT unique et des colonnes pour chaque PA_NAME répertorié dans la clause WHERE plus SAMPLE_ID et PARAM_UNITS. Donc, quelque chose comme ceci:

LOT | AL_V  | CA_V  | CO_V  | PARAM_UNITS | SAMPLE_ID 
U6X599 | 1.70000000 | 1.40000000 | 0.70000000 | PPB   | 010001459826 
U6X600 | 2.40000000 | 1.60000000 | 0.70000000 | PPB   | 010001459828 

j'ai essayé de jouer avec l'opérateur PIVOT, mais ne peut pas sembler obtenir le résultat que je cherche avec elle. Toute aide serait grandement appréciée.

Répondre

0

On dirait qu'une simple instruction de pivot standard vous conviendra.

SELECT 
    LOT , 
    AL_V , 
    CA_V , 
    CO_V , 
    PARAM_UNITS , 
    SAMPLE_ID 
FROM (
    SELECT 
     sp.SAMPLE_ID, 
     sp.PA_NAME, 
     sp.NRESULT, 
     sp.PARAM_UNITS, 
     s.LOT 
    FROM 
     DANM..SAMPLEPARAM AS sp 
     LEFT JOIN DANM..SAMPLE AS s 
      ON sp.SAMPLE_ID = s.SAMPLE_ID 
    WHERE 
     s.METHOD_SUBCLASS = 'BATCH' 
     AND sp.PA_NAME IN ('AL_V','CA_V','CO_V') 
     AND sp.ENTERED_ON > DATEADD(MONTH,-12,GETDATE())) p 
    PIVOT (max (NRESULT) FOR 
     PA_NAME IN ( 
     [AL_V], [CA_V], [CO_V])) AS pvt 
ORDER BY 
    LOT 

En marge, votre gauche est JOIN qui se transforme en INNER JOIN puisque vous êtes where contient un filtre sur le « droit » côté s.METHOD_SUBCLASS = 'BATCH'

0

Vous pouvez interroger comme ci-dessous:

Select * from #Sample 
pivot (max(NResult) for PA_Name in ([AL_V],[CA_V],[CO_V])) p 

sortie comme ci-dessous:

+--------------+-------------+--------+------+------+------+ 
| Sample_id | Param_units | LOT | AL_V | CA_V | CO_V | 
+--------------+-------------+--------+------+------+------+ 
| 010001459826 | PPB   | U6X599 | 1.7 | 1.4 | 0.7 | 
| 010001459828 | PPB   | U6X600 | 2.4 | 1.6 | 0.7 | 
+--------------+-------------+--------+------+------+------+ 

Pour une liste dynamique de colonnes peut interroger comme ci-dessous:

Declare @cols1 varchar(max) 
Declare @query nvarchar(max) 

Select @cols1 = stuff((select Distinct ','+QuoteName(PA_Name) from #Sample for xml path('')),1,1,'') 
Select @query = ' Select * from 
       (Select Sample_id, PA_Name, NResult, Param_units, LOT from #Sample) a 
       pivot (max(NResult) for PA_Name in (' + @cols1 + ')) p ' 

Exec sp_executesql @query 
0
;With Pivotdata 
As 
(
    SELECT sp.SAMPLE_ID 
     ,sp.PA_NAME -- spreading element 
     ,sp.NRESULT -- aggregating element 
     ,sp.PARAM_UNITS 
     ,s.LOT -- grouping element 
    FROM DANM..SAMPLEPARAM AS sp 
    LEFT JOIN DANM..SAMPLE AS s ON sp.SAMPLE_ID = s.SAMPLE_ID 
    WHERE s.METHOD_SUBCLASS = 'BATCH' 
     AND sp.PA_NAME IN (
      'AL_V' 
      ,'CA_V' 
      ,'CO_V' 
      ) 
     AND sp.ENTERED_ON > DATEADD(MONTH, - 12, GETDATE()) 
) 
Select 
    Lot, AL_V, CA_V, CO_V, Param_Units, Sample_ID 
From Pivotdata 
Pivot(Sum(NResult) For PA_Name In (AL_V, CA_V, CO_V)) As P;