0

Trying to make rows columns with some value underneath. I am also trying to use dynamic columns for my pivot and I am close but not there yet. so currently this is my query and this is the output of my query.

DECLARE @cols VARCHAR(max)

SELECT @cols = isnull(@cols + ', ', '') + '[' + T.name + ']'
FROM (
    SELECT name
    FROM links.project_schema
    WHERE link_id = 100550
    ) AS T

SELECT *
FROM (
    SELECT ps.name
        ,sla.store_nbr
        ,psd.schema_value
    FROM links.project_schema ps
    JOIN links.links l ON l.id = ps.link_id
    JOIN links.store_link_assignment sla ON sla.links_id = link_id
    JOIN links.project_schema_details psd ON psd.project_schema_id = ps.id
    WHERE ps.link_id = 100550
    ) b
pivot(max(name) FOR name IN (@cols)) TEMP

enter image description here

But I want it to look like this

enter image description here

the nulls are the schema values

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
josh
  • 378
  • 6
  • 20
  • `FOR name IN (@cols)` doesn't mean what you think it does. Have a look at this [answer](https://stackoverflow.com/a/10404455/2029983), which i suspect you got the code from, and notice the difference. – Thom A Mar 18 '20 at 14:50
  • you are awesome I get it now @Larnu I needed to change the max(schema_value) for name in my pivot I actually understand this now thanks to you!! how do I give you credit lol? – josh Mar 18 '20 at 15:05
  • Just seems like a typographical error, then, no need to a reward answer. – Thom A Mar 18 '20 at 15:09
  • Just show input of initial input then it would be more nice for you.don't show input of already pivoted query.do you want double pivot ? – KumarHarsh Mar 19 '20 at 06:17
  • Your query suggest that you already know ,Just store the result in #temp and again pivot it using same funda – KumarHarsh Mar 19 '20 at 06:19

0 Answers0