sql - Assign value of a column in a table as columname of another table -


I have a parent table SalType and Saltype_name Is the column name Another table StaffSal that SalType foreign key relationship with the StaffSal I enter values ​​for each SalType_name

table structure like this

  Saltaip --table 1 Ss_aid Saltaip_nam 1 Basic 2 HRA 3 DA StaffSal --table2 SV_id SS_id SV_value U_id 2 1 15000 11 3 2 0.1 11 4 3 0.75 11 5 1 10000 12 6 2 0.01 12 7 3 0.5 12   

I have to show this way

  U_id Basic HRA DA ... 11 15000 1.75 12 10000.01.5   

Can anyone help?

There are several ways that you can convert data from rows to columns.

A PIVOT function in SQL Server that can be applied to rotate the data

If you have a limited number of values ​​that you call columns You can hardly query: SRC Pivot for saltype_name in u_id, Basic, HRA, DA (Original, HRA, DA), select

  (max ( Sv_value)), excluding STOTP, except STOTP, name, SU_ID, SS_View, excluding PIV (TS_ID = s.ss_id);   

But if you have unknown numbers, then you want to apply dynamic SQL to generate results:

 SLAPAN order by SASID from SALTIPE group by SASID (XML path to) (code)) TYPE) Kvalue ( 'K', 'NVARCHAR (MAX)), 1,1' ') set @query =' sELECT u_id, '+ from @cols +' (selection Do tty_type_name, s. (for '+ saltype_name in @ Cos +') x pivot (maximum sv_value)) p 'execute (@query)   

View.

Both give result to questions:

  | U_ID | Basic | HRA | DA | ------------------------------ | 11 | 15000 | 0.1 | 0.75 | | 12 | 10000 | 0.01 | 0.5 |    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -