sql - MSSQL query to combine information in a new table -


my table:

servername: | factname: | factvalue: server01    | cpucores  | 2 server01    | memory    | 64gb server01    | hdd01size | 50gb server01    | hdd02size | 60gb server01    | hdd03size | 70gb server02    | cpucores  | 4 server02    | memory    | 32gb server02    | hdd01size | 50gb server02    | hdd02size | 60gb server02    | os        | windows 2012 r2 

what try create mssql query new table following output:

servername:|cpucores:|memory:|hdd01size:|hdd02size:|hdd03size:|os:    server01   |2        |64gb   |50gb      |60gb      |70gb      | -      server02   |4        |32gb   |50gb      |60gb      | -        |windows 2012 r2 

the columns need dynamically created during fetching of data. column names are:

select distinct [factname] [table] 

...with addition of column "servername".

i can't figure out best way accomplish this.

this referred table pivoting. here's 1 option using conditional aggregation:

select servername,      max(case when factname = 'cpucores' factvalue end) cpucores,     max(case when factname = 'memory' factvalue end) memory,     max(case when factname = 'hdd01size' factvalue end) hdd01size,     max(case when factname = 'hdd02size' factvalue end) hdd02size,     max(case when factname = 'hdd03size' factvalue end) hdd03size,     max(case when factname = 'os' factvalue end) os yourtable  group servername 

if don't know potential values/columns, you'll need use dynamic sql.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -