pivot - SQL Server query to generate dynamic columns -
i have 2 tables - 1 state , other 1 job titles.
i want write query output me this:-
job titles state name1 state name2 job title1 200 300 job title2 500 600
how can write query in sql server.
i have no idea schema looks like, sounds want change have 3 tables: jobtitle, state, , jobtitle_state_salary. way you're not repeating either job titles or states in order tie salary.
however, addressing problem written (and making assumption salary travels state), should trick:
with [cte] ( select [title], [state], [salary] [jobtitle] inner join [statesalary] on [jobtitle].[id] = [statesalary].[jobtitleid] ) select [title], [state name1], [state name2] [cte] pivot ( max([salary]) [state] in ([state name1], [state name2]) ) [p]
sqlfiddle example here.
Comments
Post a Comment