将记录值透明到具有连接表的列名

时间:2017-07-15 01:03:57

标签: sql sql-server

我有以下查询:

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  

结果数据的子集如下所示:

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

我正在尝试从PA_NAME转移记录,以便为每个唯一的LOT值和WHERE子句中列出的每个PA_NAME的列以及SAMPLE_ID和PARAM_UNITS获取单个记录。所以像这样:

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 

我尝试使用PIVOT操作员,但似乎无法获得我正在寻找的结果。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

看起来像一个非常标准的枢轴声明会想要你想要的。

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  

作为旁注,您的LEFT JOIN正在转换为INNER JOIN,因为您的where子句在“右侧”包含过滤器s.METHOD_SUBCLASS = 'BATCH'

答案 1 :(得分:0)

您可以查询如下:

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

输出如下:

+--------------+-------------+--------+------+------+------+
|  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 |
+--------------+-------------+--------+------+------+------+

对于动态列列表,您可以按如下方式查询:

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

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