January 10, 2018 at 5:48 am
I have a table "tblPerson" that only has three columns hder, hderOrder and key. The hderOrder is the order that the hder, This table is column header information only and the order of the columns. It has no source data.
tblPerson:
hder | hderOrder | Key |
Peter | 1 | 1 |
Ray | 2 | 1 |
Gill | 3 | 1 |
Jeff | 4 | 1 |
Mary | 1 | 2 |
Sue | 2 | 2 |
Jessica | 3 | 2 |
Grace | 4 | 2 |
I want to create a dynamic query that only has column names where key=1 that looks like this:
Peter Ray Gill Jeff
However the source data for the "tblPerson" is in another table name "tblPersonData" that matches up with the column names.
I want to make this dynamic because I don't know how many columns may be in the tblPerson table for a given key. So, I need a way to list all the columns headers (hder) and then dynamically add the source data that matches the hder.
I was trying a PIVOT but not sure how to structure it. Maybe a temp table as the output but because it is dynamic, not sure if temp table can be dynamic.
Please help to send me in the right direction.
Charles P.
January 10, 2018 at 6:08 am
I'd recommend that you read the following articles:
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - SQLServerCentral
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - SQLServerCentral
January 10, 2018 at 7:02 pm
Charles_P - Wednesday, January 10, 2018 5:48 AMI have a table "tblPerson" that only has three columns hder, hderOrder and key. The hderOrder is the order that the hder, This table is column header information only and the order of the columns. It has no source data.
tblPerson:
hder hderOrder Key Peter 1 1 Ray 2 1 Gill 3 1 Jeff 4 1 Mary 1 2 Sue 2 2 Jessica 3 2 Grace 4 2 I want to create a dynamic query that only has column names where key=1 that looks like this:
Peter Ray Gill JeffHowever the source data for the "tblPerson" is in another table name "tblPersonData" that matches up with the column names.
I want to make this dynamic because I don't know how many columns may be in the tblPerson table for a given key. So, I need a way to list all the columns headers (hder) and then dynamically add the source data that matches the hder.
I was trying a PIVOT but not sure how to structure it. Maybe a temp table as the output but because it is dynamic, not sure if temp table can be dynamic.
Please help to send me in the right direction.
Charles P.
Charles... you're a bit new to these forums so, first of all, WELCOME! Second of all, great description but if you'd like folks to spill some working code for you, take a look at the article at the first link in my signature line below under "Helpful Links" about how to create "readily consumable data" and there's a pretty good chance that people will respond much more quickly to your post and they'll frequently respond with tested code.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply