Jos*_*729 4 sql oracle oracle11g oracle-sqldeveloper
这是我的基本查询
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
Run Code Online (Sandbox Code Playgroud)
它会产生以下输出
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
Run Code Online (Sandbox Code Playgroud)
现在我需要的是显示除第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;
Run Code Online (Sandbox Code Playgroud)
我会的
Projects WIP days outstanding
History - SASAGRAM - MUMBAI 1386.52
History - NASIK - MUMBAI 89.92
History - AURANGABAD - MUMBAI 83.25
Run Code Online (Sandbox Code Playgroud)
但我也需要为其他两个地方展示
History - AURANGABAD - NASIK 0
History - PUNE - MUMBAI 0
Run Code Online (Sandbox Code Playgroud)
此查询仅显示我不想要的行.
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);
Run Code Online (Sandbox Code Playgroud)
并按预期提供输出
Projects WIP days outstanding
History - SASAGRAM - MUMBAI 0
Run Code Online (Sandbox Code Playgroud)
现在是我的问题.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;
Run Code Online (Sandbox Code Playgroud)
这会产生所需的输出
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
Run Code Online (Sandbox Code Playgroud)
这只是一种破旧的方式,我想知道是否有可能只是否定WHERE子句,或者做一些"整洁"的替代方法来做我想做的事情.
谢谢 !!
PS我使用SQL Developer和Oracle 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
Run Code Online (Sandbox Code Playgroud)
我认为应该这样做:
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);
Run Code Online (Sandbox Code Playgroud)
如果我正确地阅读你的问题,你想要的是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);
Run Code Online (Sandbox Code Playgroud)
正如 @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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8583 次 |
| 最近记录: |