Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to negate a WHERE clause in SQL?

This is my base query

select distinct a.projects , case when(billing_fy!=0)
then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) 
     from table1 b      
     where a.projects = b.projects 
     group by projects)
else 0 end as "WIP days outstanding"
from table1 a

and it produces the following output

Projects                        WIP days outstanding
History - AURANGABAD - NASIK    0
History - PUNE - MUMBAI         0
History - NASIK - MUMBAI        89.92
History - SASAGRAM - MUMBAI     0
History - SASAGRAM - MUMBAI     1386.52
History - AURANGABAD - MUMBAI   83.25

Now what I need is to display all rows except the 4th row. The reason why I'm using a case statement in the first place is because if I did (the billing_fy!=0 clause is to prevent an error caused by dividing by 0)

select projects,
round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) as "WIP days outstanding"
from table1
where billing_fy!=0
group by projects;

I would get

Projects                        WIP days outstanding
History - SASAGRAM - MUMBAI     1386.52
History - NASIK - MUMBAI        89.92
History - AURANGABAD - MUMBAI   83.25

but I need to show for the other 2 places as well

History - AURANGABAD - NASIK    0
History - PUNE - MUMBAI         0

This query only displays the row I don't want.

select distinct a.projects , case when(billing_fy!=0)
then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from table1 b     where a.projects = b.projects group by projects)
else 0 end as "WIP days outstanding"
from table1 a
where (projects='History - SASAGRAM - MUMBAI' AND billing_fy=0);

and gives the output as expected

Projects                        WIP days outstanding
History - SASAGRAM - MUMBAI     0

Now comes my question. Is there a way in SQL to negate the WHERE clause? Like in C++ I would just have to use a not operator in front of the clause to negate it. Because basically, I want to display all rows EXCEPT the row above.

Right now, I've solved my problem of displaying all rows except the row I don't want by using the following code

select distinct a.projects , case when(billing_fy!=0)
then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from table1 b   where a.projects = b.projects group by projects)
else 0 end as "WIP days outstanding"
from table1 a
where projects not in ('History - SASAGRAM - MUMBAI') and billing_fy!=0
union all
select distinct a.projects , case when(billing_fy!=0)
then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from table1 b   where a.projects = b.projects group by projects)
else 0 end as "WIP days outstanding"
from table1 a
where projects not in ('History - SASAGRAM - MUMBAI') and billing_fy=0
union all
select distinct a.projects , case when(billing_fy!=0)
then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from table1 b    where a.projects = b.projects group by projects)
else 0 end as "WIP days outstanding"
from table1 a
where projects='History - SASAGRAM - MUMBAI' and billing_fy!=0;

And this produces the required output

Projects                         WIP days outstanding
History - NASIK - MUMBAI         89.92
History - AURANGABAD - MUMBAI    83.25
History - AURANGABAD - NASIK     0
History - PUNE - MUMBAI          0
History - SASAGRAM - MUMBAI      1386.52

This is just a shabby way of doing it, and I'd like to know if it is possible to just negate the WHERE clause, or some "neater" alternative to do what I want to do.

Thanks !!

P.S. I use SQL Developer and Oracle 11g (just in case someone asks)

EDIT Input Values as requested

Projects                      Cost_Project  Billing_FY
History - NASIK - MUMBAI      65696067.99   54937478.46
History - NASIK - MUMBAI      41385613.61   151909546.44
History - NASIK - MUMBAI      18029488.91   216353866.92
History - AURANGABAD - MUMBAI 33191393.23   57073935.95
History - AURANGABAD - MUMBAI 52681451.68   139055661.74
History - AURANGABAD - MUMBAI 74576522.31   390092578.24
History - PUNE - MUMBAI       0             0
History - PUNE - MUMBAI       0             0
History - PUNE - MUMBAI       0             0
History - SASAGRAM - MUMBAI   107540114.08  40653734.06
History - SASAGRAM - MUMBAI   209167760.1   28823862.66
History - SASAGRAM - MUMBAI   0             0
History - AURANGABAD - NASIK  0             0
History - AURANGABAD - NASIK  0             0
History - AURANGABAD - NASIK  0             0
like image 272
Joshua1729 Avatar asked Feb 13 '12 13:02

Joshua1729


People also ask

How can you write not equal to in the WHERE clause?

The != operator is used in a WHERE statement. The !=

How do you exclude something in SQL?

EXCLUDE conditions in SQL usually appear in the WHERE clause of the statement or in the HAVING clause of an aggregate query. Some commonly used EXCLUDE operators in SQL are NOT, NOT IN, NOT LIKE, '! =', EXCEPT, NOT NULL, etc.

How can avoid in clause in SQL Server?

Solution 1 The only exception to this is when the range is fixed: if it's always the same ID values in your IN clause, then you could possibly save them in a separate table, and use a JOIN to "glue" the query together.

WHERE we Cannot use WHERE clause in SQL?

We cannot use the HAVING clause without SELECT statement whereas the WHERE clause can be used with SELECT, UPDATE, DELETE, etc. WE can use aggregate functions like sum, min, max, avg, etc with the HAVING clause but they can never be used with WHERE clause. HAVING clause is generally used with the GROUP BY.


1 Answers

I think this should do it:

select distinct a.projects , case when(billing_fy!=0)
then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from table1 b     where a.projects = b.projects group by projects)
else 0 end as "WIP days outstanding"
from table1 a
where (projects != 'History - SASAGRAM - MUMBAI' OR billing_fy != 0);
like image 121
Lamak Avatar answered Nov 01 '22 02:11

Lamak