-2

My table looks like:

id column1 column2 column3 // feilds names 

1    1        0       1    // row 

Here I need the columns names of table which contain 1 with respect to "id".

Output I required:

column1  column3

(which has data 1 with respect to id=1).

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Sai Kiran
  • 11
  • 2
  • 4
    Hi, welcome to stack overlfow, please read http://www.stackoverflow.com/help/How-to-ask.. You've mentioned c# whats your query? whats your code? what have you tried? – BugFinder Apr 11 '16 at 08:05
  • Possible duplicate of [MySQL - turn table into different table](http://stackoverflow.com/questions/15184381/mysql-turn-table-into-different-table) – Jiri Tousek Apr 11 '16 at 08:41

1 Answers1

0

You haven't mentioned what DBMS you are using; that would be useful. With PostgreSQL you achieve that by using the UNION construct, which is used to combine the result set of multiple SELECT statements:

SELECT id,column1 FROM "Table1" where id=column1
UNION 
SELECT id,column2  FROM "Table1" where id=column2
UNION 
SELECT id,column3  FROM "Table1" where id=column3;

However, this removes duplicates from the final result set. To also retrieve duplicates use the UNION ALL constrct:

SELECT id,column1 FROM "Table1" where id=column1
UNION ALL
SELECT id,column2  FROM "Table1" where id=column2
UNION ALL
SELECT id,column3  FROM "Table1" where id=column3;

One disadvantage with this approach is that you need as many select statements as there are columns. A second disadvantage is that your final result set will have the column labels of the first select statement (i.e. id, column1 in this example).