sql server - T-SQL - Value does not match at least one item in list -


I have a MS SQL table with multiple columns, one for each separate employee roles and one for each The column shows that in the present role the employee is activated, on leave, etc., etc. Due to dependence on the external data source, the fields are not actually normalized.

I am trying to run a query to return all the rows where is at least one of the 'active' column, equal to 'active' No has many potential values ​​other than active.

I know that there will be one of many ways to do this so

  SELECT * MYBABLE WHERE ISActive1 & lt; & Gt; 'Active' or ISActive 2 & lt; & Gt; 'Active' or iactive 3 & lt; & Gt; 'Active' or iactiv 4 & lt; & Gt; 'Active' or ... etc.   

Just thinking that there is a small path, possibly more efficient way to do this. I have seen many solutions to find a match in many columns, but not one to look for non-matches

I do not know that it is more elegant, but you can do something like this:

  in place of (ISActiv 1 + ISActiv 2 + ISActiv 3 + ..., 'Active' '') & lt; & Gt; ''   

Note that if the value can be NULL , then you will need to replace them with something other than this, Are not empty.

Edit:

If you want to do it efficiently and code look good, add a calculation column and an index Add table as your IAllActive (case when IsActive1 = 'ACTIVE' and IsActive2 = 'ACTIVE' and ... then 'ACTIVE' is another 'forbidden' end of the table:

  ); Create mytable_IsAllActive Index on Meritable (IsAllActive);   

You can also add other relevant columns to the index.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -