问题
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
I've attached the image of my table. This table individual ids in Strain_ID
and each Id has several markers in different loci. I want to pivot
on Loci aggregating on Marker, so that I can have individual strain_Ids
in rows and all the loci as columns.
This is the script I use on Sql server 2012 Management studio:
declare @cols varchar(Max)
declare @cols1 varchar(Max)
set @cols ='[' ;
select @cols += 'D.'+QUOTENAME (Locus) + ','
from(
select distinct Locus from genotypeQA where Chromosome IN ('Gm01')
) as X
set @cols= stuff(replace(@cols,'D.[','['),1,1,'')
print @cols
set @cols1 = SUBSTRING(@cols,1,LEN(@cols)-1)
print @cols1
select *
from (
select
genotypeQA.Strain_ID,
genotypeQA.Family,
'+ @cols +',
genotypeQA.Marker
from genotypeQA
where
genotypeQA.Family IN ('10')
AND genotypeQA.Chromosome IN ('Gm01')
) as D
Pivot(
MAX(Marker)
For Locus IN ('+ @cols +')) as p
I get the following error:
Msg 102, Level 15, State 1, Line 31
Incorrect syntax near '+ @cols +'.
I expect the following format of output shown here with part of the 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 | |
+------------+-----------+---------------+-------------+-------------+----------
回答1:
EDIT :
First of all you need to bring Variant
, Variant_A
and Variant_B
values to single column and corresponding values column name in another column, ie, you have to UNPIVOT
. I have used CROSS APPLY
instead of UNPIVOT
here.
Declare a variable to get column names for pivoted table
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + ChrLocus + ']', '[' + ChrLocus + ']')
FROM
(
SELECT DISTINCT Chromosome+'_'+ CAST(Locus AS VARCHAR(10))ChrLocus
FROM #TEMP
) PV
ORDER BY ChrLocus
Now pivot the result. I have written the logic inside the query
DECLARE @query NVARCHAR(MAX)
SET @query = '-- This outer query forms your pivoted result
SELECT * FROM
(
-- Source data for pivoting
SELECT DISTINCT Chromosome+''_''+ CAST(Locus AS VARCHAR(10))ChrLocus,Strain_ID,
Variants,COLNAMES
FROM #TEMP
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
ORDER BY Strain_ID;'
EXEC SP_EXECUTESQL @query
- Click here to view result
回答2:
You have to put the SQL into a variable and then use EXEC(@sqlCode) to execute the SQL Dynamically. eg. DECLARE @mySql AS VARCHAR(MAX) = sql pivot query here
. Then call EXEC(@sqlCode) to generate the result. Like this:
DECLARE @sqlCode AS VARCHAR(MAX);
SET @sqlCode = 'select *
from (
select genotypeQA.Strain_ID, genotypeQA.Family,'+ @cols + ' ,genotypeQA.Marker from genotypeQA where genotypeQA.Family IN ('10') AND genotypeQA.Chromosome IN ('Gm01')
) as D
Pivot( MAX(Marker) For Locus IN ('+ @cols +')) as p'
EXEC (@sqlCode)
来源:https://stackoverflow.com/questions/28100672/syntax-error-in-dynamic-sql-server-query-with-pivot-clause