sql - Column names in result and rotate part of table -
the following table structure:
'----id-----'----name----'----field1----'----field2----' ' 1 ' val ' 123 ' 321 ' ' 2 ' val2 ' 234 ' 212 '
need following result:
'----id-----'----name----'----fields----'----values----' ' 1 ' val ' field1 ' 123 ' ' 1 ' val ' field2 ' 321 ' ' 2 ' val2 ' field1 ' 234 ' ' 2 ' val2 ' field2 ' 212 '
how write query? can column names information_schema.columns
. how join table information_schema.columns
? how can rotating part of table?
as living example. following table:
on screenshot several fields in table there lot of fields. wrote following query:
select p.guid, p.myvalues, p.fields mytable gz unpivot( [myvalues] fields in ([area], [davlplastmax])) p
but query doesn't return null values.
also want columns information_schema.columns
, past them in ([area], [davlplastmax])
.
for example:
unpivot( [values] fields in ( select [mydb].information_schema.columns.column_name [mydb].information_schema.columns table_name = n'mytable' )
unpivot?
select u.id, u.name, u.fields, u.values mytable t unpivot ( values fields in (field1, field2) ) u;
Comments
Post a Comment