带有pivot的SQL Server子查询

时间:2015-03-19 21:08:24

标签: sql-server subquery pivot

Chromosome  Locus       Variant_A   Variant_B   Variant Strain_ID   Family  Parent1_Name    Parent1_Marker  Parent2_Name    Parent2_Marker  Line    Marker  Gid
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Gm09        40907915    G           A           GA      DS11.46096  46      IA3023          AA              PI507.681B*     BB              96      BB      2
Gm09        422384      G           A           GA      DS11.46096  46      IA3023          AA              PI507.681B*     BB              96      AA      4
Gm09        422720      A           G           AG      DS11.46096  46      IA3023          BB              PI507.681B*     AA              96      BB      5
Gm09        424439      C           A           CA      DS11.46096  46      IA3023          AA              PI507.681B*     BB              96      AA      7
Gm09        425375      G           T           GT      DS11.46096  46      IA3023          AA              PI507.681B*     BB              96      AA      9
Gm09        425581      T           C           TC      DS11.46096  46      IA3023          BB              PI507.681B*     AA              96      BB      10
Gm09        43921862    C           A           CA      DS11.46096  46      IA3023          BB              PI507.681B*     AA              96      AA      12

这是我的表。我需要这个输出:

| Strain     |           | Gm09_40907915 | Gm09_422384 | Gm09_422720 | Gm09_424439 |    |
| DS11.46096 | Variant_A | G             | G           | A           | C           |    |
| DS11.46096 | Variant_B | A             | A           | G           | A           |    |
| DS11.46096 | Variant   | GA            | GA          | AG          |        CA    |  |

+ ------------ + ----------- + --------------- + ----- -------- + ------------- + ----------

当我使用此代码时:

use nam13;

DECLARE @cols NVARCHAR (MAX)

SELECT @cols = COALESCE (@cols + ',[' + ChrLocus + ']', '[' + ChrLocus + ']')
           FROM    
           (
                 SELECT DISTINCT Chromosome+'_'+ CAST(Locus AS VARCHAR(10))ChrLocus 
                 FROM genotypeQA 
           ) PV 
           ORDER BY ChrLocus

现在转动结果。我在查询中编写了逻辑

DECLARE @query NVARCHAR(MAX)

SET @query = (SELECT *
         FROM 
         (  -- Source data for pivoting
            SELECT Chromosome+'_'+ CAST(Locus AS    VARCHAR(10))ChrLocus,Strain_ID,
            Variants,COLNAMES 
            FROM genotypeQA
            CROSS APPLY(VALUES (Variant_A,'Variant_A'),(Variant_B,'Variant_B'),(Variant,'Variant'))
            AS COLUMNNAMES(Variants,COLNAMES)) x

         PIVOT 
         (
             --Defines the values in each dynamic columns
             MIN (Variants)

             -- Get the names from the @cols variable to show as column

             FOR ChrLocus IN (['+ @cols +'])
        ) p            
    );

EXEC SP_EXECUTESQL @query

我在SQL Server中收到以下错误:

  

Msg 116,Level 16,State 1,Line 36
  当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

1 个答案:

答案 0 :(得分:0)

由于您要动态构建查询,因此您希望将@query用作字符串并将@cols注入其中。

我也会对查询进行一些小的更改(以获得正确的变体类型),下面的查询应该为您提供所需的输出:

DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + ChrLocus + ']', '[' + ChrLocus + ']')
FROM    
(
        SELECT DISTINCT Chromosome+'_'+ CAST(Locus AS VARCHAR(10))ChrLocus 
        FROM genotypeQA 
) PV 
ORDER BY ChrLocus

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT Strain_ID, COLNAMES as Variants, ' + @cols + '
    FROM 
         (  -- Source data for pivoting
            SELECT CONCAT(Chromosome,''_'',Locus) ChrLocus,Strain_ID,
            Variants, COLNAMES, sort
            FROM genotypeQA
            CROSS APPLY(VALUES (1, Variant_A,''Variant_A''),(2, Variant_B,''Variant_B''),(3, Variant,''Variant''))
            AS COLUMNNAMES(Sort, Variants,COLNAMES)
          ) x

         PIVOT 
         (
             --Defines the values in each dynamic columns
             min (Variants)

             -- Get the names from the @cols variable to show as column

             FOR ChrLocus IN ('+ @cols +')
        ) p 
       order by strain_id, sort
    ;'

--print @query
EXEC SP_EXECUTESQL @query

输出结果为:

Strain_ID  Variants  Gm09_40907915 Gm09_422384 Gm09_422720 Gm09_424439 Gm09_425375 Gm09_425581 Gm09_43921862
---------- --------- ------------- ----------- ----------- ----------- ----------- ----------- -------------
DS11.46096 Variant_A G             G           A           C           G           T           C 
DS11.46096 Variant_B A             A           G           A           T           C           A 
DS11.46096 Variant   GA            GA          AG          CA          GT          TC          CA