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