0

I created a Dynamic SQL query which joins a few tables, and then pivots off a table called Geometries which stores Name/Value pairs. The SQL is dynamic, because at run-time I won't know which name/value pairs are required to pivot off of.

The query below gets me the data I need, however, all of the geometry columns are listed twice as if I pivoted twice off the same data. I cannot figure out why this happening. I suspect it has to do with the fact that I select the Name/Value pairs in my common table expression which selects the Geometry Name/Value columns on line 13 of the code sample below: g.Name[Geometry], g.Value

Maybe coupling that with the PIVOT code near the bottom is causing the duplicate column data in my query results?

I can't remove the code on line 13, or the PIVOT fails with errors saying Geometry and Value are not valid column names.

Anyway, here is the query...

DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME(g.Name) 
                    FROM Geometries g
                    FOR XML PATH(''), TYPE
                    ).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'')
SET @query = 
    'SELECT *, ' + @cols + ' 
    FROM (
            SELECT 
                r.Id[RunId], rd.Id[RunDataId], r.RunNumber [Run #], r.TestNumber [Test #], r.Description,
                rd.data1, rd.data2, rd.data3,
                g.Name[Geometry], g.Value
            FROM dbo.Runs r
                INNER JOIN RunDatas rd ON r.Id = rd.RunId
                INNER JOIN RunGeometries rg ON rg.RunId = r.Id
                INNER JOIN Geometries g ON g.Id = rg.GeometryId
        ) as data
            PIVOT
            (
                Max(Value) FOR Geometry IN (' + @cols + ')
            ) as p'

execute sp_executesql @query

Any help figuring out what is causing the duplicate data would be appreciated.

1
  • Probably your tables and not your PIVOT. Do a SELECT DISTINCT r.ID, rd.ID etc... to make sure you aren't bringing dupes for PIVOT Commented Sep 12, 2017 at 18:48

1 Answer 1

1

select * is including the columns you are pivoting; try this:

DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME(g.Name) 
                    FROM Geometries g
                    FOR XML PATH(''), TYPE
                    ).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'')
SET @query = 
    'SELECT [RunId], [RunDataId], [Run #], [Test #], Description, data1, data2, data3, ' + @cols + ' 
    FROM (
            SELECT 
                r.Id as [RunId], rd.Id as [RunDataId], r.RunNumber as [Run #], r.TestNumber as [Test #], r.Description,
                rd.data1, rd.data2, rd.data3,
                g.Name[Geometry], g.Value
            FROM dbo.Runs r
                INNER JOIN RunDatas rd ON r.Id = rd.RunId
                INNER JOIN RunGeometries rg ON rg.RunId = r.Id
                INNER JOIN Geometries g ON g.Id = rg.GeometryId
        ) as data
            PIVOT
            (
                Max(Value) FOR Geometry IN (' + @cols + ')
            ) as p'
print @query -- you can also take a look at the code you are generating to help troublshoot
execute sp_executesql @query

You can also check the code that is being generated with print @query or select @query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.