Sunday 15 January 2012

SQL Server - Query to join two tables based on columnName field -



SQL Server - Query to join two tables based on columnName field -

i have existing sql server table construction following:

--entries table-- *company* | *musicproduct* | *foodproduct* | *pcproduct* | *phoneproduct* | apple ipod null imac iphone dell null null dellbook null

the primary key on above table company

--questions table-- *columnname* | questions musicproduct mp3 device company known for? foodproduct nutrient product describes company best? pcproduct pc? phoneproduct phone company give employees?

the primary key on above table columnname , contains row each of non-key columns in first table. feels bad design, preexists work.

i want query homecoming rows question , answer, ignoring questions null answers, chosen single company. output apple (three rows):

question reply mp3 device company known for? ipod pc? imac phone company give employees? iphone

how can best accomplish above? sort of new reference table way go?

please seek this.

select q.question, product reply (select company, musicproduct, foodproduct, pcproduct, phoneproduct entries ) p unpivot ( product products in (musicproduct, foodproduct, pcproduct, phoneproduct) ) unpvt bring together questions q on unpvt.product = q.answer;

sql sql-server

No comments:

Post a Comment