SQL Where Clause To Match Against Both Conditions Simultaneously - sql

I don't know how to phrase my question title for what I'm about to ask.
I have a SELECT query that must not return any rows if the combination of my where clause is true. Here is my example code:
SELECT
*
FROM
MyTable m1
WHERE
(m1.User != '1' AND m1.Status != '1')
But what I am trying to ask SQL is:
"only return rows when the User is not '1' AND his status is not '1' at the same time. If this combination is not true, then its okay to return those rows".
So if the User is "1" and Status is "2" then that is fine to return those rows.
Seems simple but I can't visualize how to do it... help please?

Just answered my own question.... here is the answer. 'OR' doesn't test for combination of both being true.
Solution:
SELECT
*
FROM
MyTable m1
WHERE NOT
(m1.User = '1' AND m1.Status = '1')
Because both conditions have to be true for it not to return the rows. Both = AND, Either = OR.

You may try this using OR instead of AND:
SELECT
*
FROM
MyTable m1
WHERE
!(m1.User = '1' OR m1.Status = '1')

SELECT
*
FROM
MyTable m1
WHERE
(m1.User <> '1' AND m1.Status <> '1')
There are many ways of doing this :)

Related

Converting SQL to linq - ISNULL [duplicate]

I have a Query in SQL Server :
SELECT * FROM MyTable t
WHERE ISNULL(t.Status,'') = ''
How I can do it in Entity Framework?
EDIT:
Oh Sorry my code was like
WHERE ISNULL(t.Status,'') = ''
Try something like
MyTable.Where( t => (t.Status ?? "") == "CO" )
Although the question is ok, the logic isn't sound.
Because if a value is equal to CO, it can never be equal to either NULL or ''.
In this case you could just easily call it like this:
SQL:
SELECT * FROM MyTable t
WHERE t.Status = 'CO'
Linq:
var items = (from t in db.MyTable
where t.Status == "CO"
select t);
However if you would need it to have a default value when NULL and compare to that value it would make more sense (see example):
SQL:
SELECT * FROM MyTable t
WHERE ISNULL(t.Status, 'CO') = 'CO'
Linq:
var items = (from t in db.MyTable
where (t.Status ?? "CO") == "CO"
select t);
This would give you all items where t.Status is NULL or equal to CO.
This is, of course, just an example.
Note: The generated sql would probably be slightly different, but the result is the same.
It would probably look something like this:
SELECT * FROM MyTable t
WHERE COALESCE(t.Status, 'CO') = 'CO'

multiple values to oracle case statement then

Can some one please explain how to pass multiple values to oracle case statement Then
SELECT *
FROM impl_debitor_information
WHERE soft_delete='F'
AND SHOP_ID ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND (debitor_type IS NULL
OR debitor_type IN (CASE
WHEN (SELECT techfund_debitor_enabled
FROM impl_shop
WHERE shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099') = 'YES' THEN ('T','D')
ELSE 'D'
END))
If values from
select techfund_debitor_enabled from impl_shop where shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099' is "YES" then I need to pass 2 values to in clause, if not single value
Thanks in advance
CASE will only return a single value. You must rewrite your query. Something like this:
SELECT *
FROM impl_debitor_information i, impl_shop where shop_id s
WHERE d.soft_delete='F'
AND d.shop_id ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND d.shop_id = s.shop_id
AND (d.debitor_type IS NULL
OR (d.debitor_type IN ('T','D') AND s.techfund_debitor_enabled = 'YES')
OR (d.debitor_type IN ('D') AND s.techfund_debitor_enabled <> 'YES'))
There might be errors in it, I didn't test the query.

Case when statement in SQL

I am using the following query. In this query I want to apply the where clause based on passed parameter. But the issue is that where clause is like 'value = if parameterVal = 'I' than NULL else NOT NULL'
I've build a query like this
SELECT * FROM MASTER
WHERE
Column1 IS (CASE WHEN :Filter = 'I' THEN 'NULL' ELSE 'NOT NULL' END)
but it's not working. Help me solve this.
UPDATE
Updating question to elaborate question more clearly.
I've one table MASTER. Now I am passing one parameter in query that is Filter (indicated by :Filter in query).
Now when the Filter parameter's value is 'I' than it should return the following result.
SELECT * FROM MASTER WHERE Column1 IS NULL
but if the passed argument is not equal to 'I' than,
SELECT * FROM MASTER WHERE Column1 IS NOT NULL
SELECT * FROM MASTER
WHERE (Filter = 'I' AND Column1 IS NULL)
OR
(Filter <> 'I' AND Column1 IS NOT NULL)
If you really insist on using a CASE the SELECT could be rewritten as:
SELECT *
FROM MASTER
WHERE CASE
WHEN COLUMN1 IS NULL AND FILTER = 'I' THEN 1
WHEN COLUMN1 IS NOT NULL AND FILTER <> 'I' THEN 1
ELSE 0
END = 1
SQLFiddle here
Frankly, though, I think that this is very difficult to interpret, and I suggest that #MAli's version is better.
Your case has assignment not equality check

Using case when - Is this possible?

Right off the bat - I'm quite new to 'case when'. I read the following: How do I perform an IF...THEN in an SQL SELECT? however it didn't really answer my question.
Essentially what I'm trying to do is something along the lines of the following:
select
section_name, *
from
property.lease_period lp
where
lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding')
and lp.period_id = #period_id
and lp.building_id = #building_id
and not exists
(
select 1
from lease_deal.lease
where lp.suite_name = tenancy_reference
and lp.building_id = building_id
)
case when(#section_name <> 'ALL')
then(and upper(section_name) = upper(#section_name))
end
order by period_id desc
Is this possible? If so what am I doing wrong?
Tl;dr:
Essentially I would like:
and upper(section_name) = upper(#section_name)
To only apply to my where clause when #section_name is not equal to 'ALL'
You just can change your (non-working) CASE to
AND (#section_name = 'ALL' OR upper(section_name) = upper(#section_name))
This can be done in a simpler way without the need to use CASE. It will be something like this:
and ((upper(section_name) = upper(#section_name) and #section_name <> 'ALL') OR #section_name ='ALL')
AND upper(section_name)=CASE WHEN #section_name <> 'ALL' THEN upper(#section_name)
ELSE upper(section_name)
END

Order by Maximum condition match

Please help me to create a select query which contains 10 'where' clause and the order should be like that:
the results should be displayed in order of most keywords(where conditions) matched down to least matched.
NOTE: all 10 condition are with "OR".
Please help me to create this query.
i am using ms-sql server 2005
Like:
Select *
from employee
where empid in (1,2,4,332,434)
or empname like 'raj%'
or city = 'jodhpur'
or salary >5000
In above query all those record which matches maximum conditions should be on top and less matching condition record should be at bottom.
SELECT *
FROM (SELECT (CASE WHEN cond1 THEN 1 ELSE 0 END +
CASE WHEN cond2 THEN 1 ELSE 0 END +
CASE WHEN cond2 THEN 1 ELSE 0 END +
...
CASE WHEN cond10 THEN 1 ELSE 0 END
) AS numMatches,
other_columns...
FROM mytable
) xxx
WHERE numMatches > 0
ORDER BY numMatches DESC
EDIT: This answer was posted before the question was modified with a concrete example. Marcelo's solution addresses the actual problem. On the other hand, my answer was giving priority to matches of specific fields.
You may want to try something like the following, using the same expressions in the ORDER BY clause as in your WHERE clause:
SELECT *
FROM your_table
WHERE field_1 = 100 OR
field_2 = 200 OR
field_3 = 300
ORDER BY field_1 = 100 DESC,
field_2 = 200 DESC,
field_3 = 300 DESC;
I've recently answered a similar question on Stack Overflow which you might be interested in checking out:
Is there a SQL technique for ordering by matching multiple criteria?
There are many options/answers possible. Best answer depends on size of the data, non-functional requirements, etc.
That said, what I would do is something like this (easy to read / debug):
Select * from
(Select *, iif(condition1 = bla, 1, 0) as match1, ..... , match1+match2...+match10 as totalmatchscore from sourcetable
where
condition1 = bla or
condition2 = bla2
....) as helperquery
order by helperquery.totalmatchscore desc
I could not get this to work for me on Oracle.
If using oracle, then this Order by Maximum condition match is a good solution.
Utilizes the case when language feature