2

I am building a query where I will need a UNPIVOT on dynamic columns. (abcd are example string name)

data1 data2 com   fr random
  1     2    a    d    sq
  3     4    b    a    fd

UNPIVOT like so :

data1 data2 Name Website random
  1     2    a     com     sq
  1     2    d     fr      sq
  3     4    b     com     fd
  3     4    a      fr     fd

The matter here is for building my First table I use dynamic SQL (@QueryFinal) because of the column. Here is my UNPIVOT dynamic Query

'SELECT data1, data2, Name, Website
FROM '+@QueryFinal+'
UNPIVOT (
         Name FOR Website in ('+@WebsiteCol+')
         ) f;'

In my @QueryFinal I have a WHERE .... ORDER BY, it seems like the UNPIVOT can't handle it. When I delete the WHERE and ORDER BY clause I get the error :

Incorrect syntax near the keyword 'UNPIVOT'.
1
  • PRINT is your friend. Have a look at the output of PRINT @YourDynamicSQLVariable; and try to debug it; if you're having trouble post the output in your question. Commented Jul 25, 2018 at 9:31

2 Answers 2

2

Try the following dynamic-pivot:

--drop table if exists unpivottest
create table unpivotTest (data1 int, data2 int, com char(1), fr char(1))

insert into unpivotTest 
select 1, 2, 'a' , 'd' union all
select 3, 4, 'b', 'a' 

select * from unpivotTest

declare @colsunpivot as nvarchar(max),
   @query  as nvarchar(max)

select @colsunpivot = stuff((select ','+ quotename(c.name)
                             from sys.columns c
                             where c.object_id = object_id('dbo.unpivottest') and c.name not like '%data%'
                             for xml path('')), 1, 1, '')

set @query 
          = 'select data1, data2, name, website
             from unpivottest
             -- you cannot do the ordering here
             unpivot
             (
                name
                for website in ('+ @colsunpivot +')
             ) u
            where data1 = 1 -- here you can use your where clause
            order by data1' -- here you can do the ordering by any col
--print @query
exec sp_executesql @query;

Check a working demo here.

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

7 Comments

The '@colsunpivot' represent ALL the columns of my First table ? Because I have other columns than 'com' and 'fr' which I don't want to UNPIVOT, my '@colsunpivot' represent all the Website columns I want to unpivot
Not all of them, all except the ones with data in the name. Notice the condition and c.name not like '%data%' inside the stuff function. That is so that we only take into account the com & fr cols when un-pivot-ing. If you have more such columns, for ex. us, en etc they will also be included dynamically.
i see but I don't see any difference with what I've done... The 'unpivottest' query has a WHERE and ORDER BY clauses. My '@WebsiteCol' represent all the columns I want to UNPIVOT. '@WebsiteCol' = fr, com, ru, de
You should post the full script for your query. Maybe we can find the issue by looking at all of it.
I think you are doing the order by in the wrong place. You can order the result set after unpivoting it. I will update the answer.
|
0

Even if it isn't the same column name as in the example here is the final Query built thanks for the help. I put the WHERE clause in the first SELECT and the ORDER BY in the UNPIVOT

DECLARE @QueryFinal VARCHAR(max) = @Query + @QueryBis  + '
from #CALENDAR_FINAL temp
LEFT JOIN #BURSTS b on b.bur_id = temp.bur_id
LEFT JOIN digital_calendar_status dcs ON dcs.dcs_date = temp.[Date] AND dcs.dif_id = '+convert(varchar(2),@FormatId)+'
LEFT JOIN digital_calendar_event dce ON dce.dce_date = temp.[Date]
WHERE '+@ConditionConflict+@ConditionIcon+@ConditionDate

DECLARE @Pivot VARCHAR(2000)='
SELECT 
DateStr, f.Conflict, f.dcs_id, f.Status, f.Website, f.Advertiser,  f.Comment, f.EventName, f.EventIcone
FROM ('+@QueryFinal+') AS a
UNPIVOT
(
Advertiser
FOR Website IN ('+@WebsiteCol+')
) f
ORDER BY Date;
'

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.