0

I have a SQL table with many columns (40+). The first column is an ID column and the rest are columns which only take values of 0 or 1. Is there a query in which i can search the row and see which "cells" are equal to 1 and return the column name?

For example, say my ID is 5 and columns 4,9,23 each have a 1 for that row (all others are zero). Therefore i would want to return "Column4","Column9" and "Column23" as my result. I need the ID stipulation in there too so I know which row to examine.

Any ideas? Thanks!

mcfly
  • 1,151
  • 4
  • 33
  • 55
  • Will you explain what you are trying to accomplish with this type of table structure? There may be a better way to achieve your goal. – George Cummins Sep 10 '14 at 20:12
  • This is more of a question of being able to find a way to accomplish the goal with the given table structure. It seems like something close to `SELECT * FROM table.columns WHERE id=5 AND table.*.value = 1` would work. I'm just not quite sure on the appropriate terminology. – mcfly Sep 10 '14 at 20:16
  • I was trying to build up an answer based on [this post](http://stackoverflow.com/a/16622414/2589202). But sqlfiddle doesnt support it. Perhaps you can modify it to work for you. – crthompson Sep 10 '14 at 20:22
  • Do you want to return just one row for you example, or should it be a row per column? – Bulat Sep 10 '14 at 20:28

0 Answers0