SQL 带枢轴的服务器子查询

SQL Server sub query with 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

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

| 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 服务器中收到以下错误:

Msg 116, Level 16, State 1, Line 36
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

由于您正在动态构建查询,因此您希望将 @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