skip to Main Content

I am trying to count the amount of times an int or string value is presented in a table row from several columns in a non-normalized table.

SELECT count(*) from persons
WHERE ‘1’ IN (column 1, column 2, column 3,column 4) AND person_MyId=’14′;

what I trying to solve is if its possible to count the amount of times the value of 1 was located in the 4 columns WITHOUT normalizing that table? trying to see what I am missing in my query to pull that off,


2

Answers


  1. Use query:

    SELECT count(*) 
    from persons 
    WHERE person_MyId='14'
    AND (
         column_1 like '%1%'
      OR column_2 like '%1%'
      OR column_3 like '%1%'
      OR column_4 like '%1%'
    );
    
    Login or Signup to reply.
  2. Use SUM,

    SELECT SUM( (col1 = 1) +  (col2 = 1) +  (col3 = 1) + (col4 = 1) ) AS totCount
    FROM persons
    WHERE person_MyId = 14;
    

    See example

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search