Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL See Whether Two or More Columns In a Table is Greater Than 0

I have ran in to a little problem and would appreciate any help.

My Table is such:

CASH | CREDIT CARD | DEBIT CARD | ACCOUNT | OTHER
-------------------------------------------------
0.00   0.00          0.00         0.00      0.00

1.00   0.00          0.00         0.00      0.00

2.00   1.00          0.00         0.00      0.00

My aim is to SELECT * FROM any of the above rows that have more than one column > 0.

So the third row would be selected in this scenario with the above table.

like image 880
Barry D. Avatar asked May 21 '15 13:05

Barry D.


1 Answers

SELECT 
  [CASH], [CREDIT CARD], [DEBIT CARD], [ACCOUNT], [OTHER]
FROM table
WHERE
  CASE WHEN [CASH] > 0 THEN 1 ELSE 0 END+
  CASE WHEN [CREDIT CARD] > 0 THEN 1 ELSE 0 END+
  CASE WHEN [DEBIT CARD] > 0 THEN 1 ELSE 0 END+
  CASE WHEN [ACCOUNT] > 0 THEN 1 ELSE 0 END+
  CASE WHEN [OTHER] > 0 THEN 1 ELSE 0 END >= 2
like image 104
t-clausen.dk Avatar answered Oct 13 '22 20:10

t-clausen.dk