Select data from one table & then rename the columns based on another table in SQL server

SharpCoder

I have two tables. TableOne which contains two columns (name & value). TableTwo can contain N no. of columns. Number of rows in TableOne will be equal to number of columns in TableTwo.

see the below image for more information.

enter image description here

What I want:

When I run select query on TableTwo, the result-set should pick the column names based on value column of TableOne. We need to match column name of TableTwo with rows available in TableOne and perform transform.

So the output should look like this:

ColumnOne | ColumnTwo | columnThree | ColumnFour

 1           1            1             2015-05-08 15:28:22.630
 2           2            2             2015-05-07 15:28:22.630
 ................
 ................
Sam Abushanab

You can use dynamic sql to generate the query to execute based on the values in the first table. Here is an example:

Declare @dynamicSQL nvarchar(200)

SET @dynamicSQL = 'SELECT ' + (SELECT stuff((select ',' + name + ' AS ' + value from Table1 for xml path('')),1,1,'')) + ' FROM Table2'

EXECUTE sp_executesql @dynamicSQL

SQL Fiddle: http://sqlfiddle.com/#!6/768f9/10

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server : can't select columns of one table by filtering from another table on same database issue

SQL Select all columns from one table and Max value of another column on another table

How to select all records from one table that exactly match another table in SQL Server?

How to copy data from one table to another table based on criteria

Append columns from one data table to another data table

Filtering data values in one column based on another column and then inserting values into different columns in same SQL Table

Display two columns but one of them is in union with another column from another table in SQL Server

link a value from one table to another and slice one table based on columns from another table in sql

select rows from table based on data from another table

How to Update Table1 two columns from another table select query in MS SQL Server

UPDATE one SQL table based on equality of two columns with two columns from another table

Pulling data from one table to use in another query for SQL Server?

SQL server : Inserting/updating missing data from one table to another

How To Populate One Column Based On Another Columns Data In Same Table?

SQL query to select row from one table which is not in another table

while creating a table select one column from another table sql

Can i copy data from one database table to another already existing database table sql server?

SQL server 2008 query move table data from one to another

SQL Server Copy Random data from one table to another

select data from one column based on another column's condition in the same table and insert that resulting data to another table

how to copy only data from one database table to another database existing table in sql server

Select all columns from one table and 1 column from another

Select data from one table based on selected rows in another

Data from one table to select data columns from another table, using r

Select data from one table base on selection from another table in SQL

SQL: select rows from a certain table based on conditions in this and another table

Select rows from R table based on two columns in another table

Select all data from one table that does not exists in another table given 3 key columns

Select data from one table depending on the results of another select SQL Server