I have a known number, x and several named columns (ix, col1, col2, col3...) and need to select a single value from either col1, col2, or col3 from each row (not necessarily the same column) where the total adds up to the known number x. I am having a difficult time trying to figure out the logic for this.

There is no chance of multiple result sets using the numbers in the columns, but one column from each row MUST be returned, with all columns returned adding up to the number X.

Ideally it would be something like this:

SELECT * as results FROM table WHERE sum(results) = number

however I can't use * in the sum statement, and it is possible that more than one result from each column (although being in separate rows) could be returned. SO I can't do Select col1, col2 col3 etc. As well, the table has a primary key IX and 64 columns, so any select case statement, or such won't work

put on hold as unclear what you're asking by Paul White Aug 21 at 12:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


May be it will help you

SELECT * as results FROM table HAVING sum(col1) = number OR sum(col2) = number OR sum(col3) = number


Not the answer you're looking for? Browse other questions tagged or ask your own question.