How can I do a cross tab
select t1.id, ti.value
from table1 t1
left Join table2 t2
on t2.jobid = t1.jobid
and t2.QuestionID = 'type_run'
left Join ResumeDataODS..ResumeResponse rTarg (nolock)
where t1.ID = 'time_type' AND
t2.ID = 'type_run'
ID value id value
---------------- ---------------- ----------------- ------------
time_type Hour type_run 15.00
time_type YEAR type_run 25,000
time_type YEAR type_run 35,000
time_type Hour type_run 12.00
time_type YEAR type_run 68,000
how can I get these value column on the same grid
15.00 , 25,000, 35,000, 12.00 68,000
Open in new window
nor is it even legal T-SQL. I realize you were just sjowing mock-up data. rTarg is not even used.
venk_r and lochan both provided solutions which require you to know name the columns in advance, but I can imagine what you really want means you may not know what they are, nor how many of them there will be.
Presumably what you are asking for is really something like this;
Open in new window
but you can see the column headings must be named something that uniquely identifies the row from which the data came. Or perhaps you even want some row values aggregated. In the example above, notice where I have had to number the duplicated column headings with [1], [2], [3] to keep them unique? You could generate exactly the results above, and dynamically create aditional numbered columns as your pivoted rowcount increased, but even then, the question is; what do the column values even mean? The are in no particular order, and have no particular relationship to one another. And some jobid's may have 10 results, while others have 3.
All can be handled, but you need to be clearer about what you are expecting.
Or maybe you don't actually want the numbers listed as seperate columns. Maybe you just need 2 columns
Open in new window
which will be simpler to do than anything so far.