2017-02-13 69 views
0

我有一个图表,我想显示资源名称和他们正在处理的请求数量为每个状态类型。例如约翰史密斯正在进行中5,4 - 未开始,5 - 分析,0 - 暂停。SQL返回零的记录未找到

我的查询为John Smith提取所有记录,但只显示存在的记录。如何强制结果表显示John Smith处于保持状态的零点?

我想展示的结果是10名员工,每位员工都显示我可用的所有状态(未开始,进行中,暂停,分析,延期等),如果没有项目处于特定阶段,则为零为那个资源。

下图显示了最左侧的资源,但如果某些没有特定状态,则不显示。即使它为零,我也希望显示所有状态。

Image

+1

请标记你的DBMS – JohnHC

+0

什么是你的表结构? – kbball

+0

编辑您的问题并显示您当前正在使用的查询,以及样本数据和所需结果。使用您正在使用的数据库进行标记。 –

回答

0

使用左外连接和组如果没有记录存在反对“约翰·史密斯”的result.You将获得空。

尝试何时将null转换为零。

0

作为一个概念,产生一个所有员工和可用状态的列表,然后将数据加入到此。这将为每个人提供一行,即使该员工不存在该行。

SQL服务器/甲骨文

with staff as 
(
select staffname, status 
from (select distinct staffname from table1) 
cross join (select distinct status from table1) 
) 
select staff.*, table1.OtherColumns 
from staff 
left join table1 
    on staff.staffname = table2.staffname 
    and staff.status = table2.status 

的MySQL:

select staff.*, table1.OtherColumns 
from 
    (
    select staffname, status 
    from (select distinct staffname from table1) 
    cross join (select distinct status from table1) 
) staff 
left join table1 
    on staff.staffname = table2.staffname 
    and staff.status = table2.status