簡體   English   中英

有沒有辦法在SQL中否定WHERE子句?

[英]Is there a way to negate a WHERE clause in SQL?

這是我的基本查詢

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

它會產生以下輸出

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

現在我需要的是顯示除第4行以外的所有行。 我首先使用case語句的原因是因為如果我這樣做(billing_fy!= 0子句是為了防止由除以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;

我會的

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

但我也需要為其他兩個地方展示

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

此查詢僅顯示我不想要的行。

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);

並按預期提供輸出

Projects                        WIP days outstanding
History - SASAGRAM - MUMBAI     0

現在是我的問題 SQL中有沒有辦法否定WHERE子句? 就像在C ++中一樣,我只需要在子句前使用not運算符來否定它。 因為基本上,我想顯示除上面行之外的所有行。

現在,我已經解決了使用以下代碼顯示除了我不想要的行之外的所有行的問題

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;

這會產生所需的輸出

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

這只是一種破舊的方式,我想知道是否有可能只是否定WHERE子句,或者做一些“整潔”的替代方法來做我想做的事情。

謝謝 !!

PS我使用SQL DeveloperOracle 11g (以防萬一有人問)

按要求編輯輸入值

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

我認為應該這樣做:

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);

如果我正確地閱讀你的問題,你想要的是not運算符:

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 not (projects='History - SASAGRAM - MUMBAI' AND billing_fy=0);

正如@ShannonSeverance指出的那樣,如果在任一字段中都有空值,這將導致問題,因為not (false and null)計算結果為null,這將被視為false。 如果你需要使這個null安全,以便它只排除具有這兩個值的行,你需要做這樣的事情:

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 (not (projects='History - SASAGRAM - MUMBAI' AND billing_fy=0))
      or projects is null 
      or billing_fy is null;

請嘗試以下方法:

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

我認為做你想做的最簡單的方法是分組,而不是使用不同的方法:

select a.projects, 
       case when sum(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
group by a.projects

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM