向SQL查询添加缺少的行

Jor*_*vis 0 sql sql-server

我有一个数据集,我需要每个问题有3行.每种状态可能为1.以下是我目前拥有的一个例子.

Issue  Status  Time 
-------------------
1      SLM      30
1      SNB      43
1      EOB      22
2      SLM      12
2      EOB      87
Run Code Online (Sandbox Code Playgroud)

我需要这样的东西,问题没有状态,然后添加一行,并设置0时间.

 Issue   Status    Time 
 ----------------------
    1      SLM      30
    1      SNB      43
    1      EOB      22
    2      SLM      12
    2      EOB      87
    2      SNB      0
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Vam*_*ala 5

使用状态交叉连接id,然后将表连接到该表.

select i.issue,s.status,coalesce(t.time,0) as time
from (select distinct status from tbl) s --replace this with status table if you have one
cross join (select distinct issue from tbl) i
left join tbl t on t.issue=i.issue and t.status=s.status
Run Code Online (Sandbox Code Playgroud)