0

我正在尝试针对reportportal v5 的postgresql 数据库编写一个sql,并发现没有明确的方法可以将project_id [和扩展的project_name] 放到与test_item 相同的行上。

我最初写了这个查询

select
    tir.result_id, 
    ti.start_time, 
    tir.end_time, 
    tir.duration, 
    ti.item_id as test_item_id, 
    cq.issue_name,
    STRING_AGG(l.log_message, '\t') as log_message 
from test_item_results tir
left join test_item ti
    on tir.result_id = ti.item_id 
left join issue i
    on tir.result_id = i.issue_id    
join (select "name", project_type, issue_type_id, issue_group_id, issue_name from project p 
        join issue_type_project itp
            on p.id = itp.project_id 
        join issue_type it 
            on itp.issue_type_id = it.id 
        where project_type = 'INTERNAL') cq
    on i.issue_type = cq.issue_type_id  
left join log l
    on tir.result_id = l.item_id
where tir.status = 'FAILED'
and type IN ('STEP')
and cq.issue_name <> 'To Investigate'
and cq.issue_name in ('Product Bug', 'Automation Bug', 'System Issue', 'No Defect')
and l.log_message is not NULL
group by tir.result_id, ti.start_time, tir.end_time, ti.item_id, tir.result_id, i.issue_id, cq.issue_name
order by ti.start_time desc

但是,固定故障类型:产品错误、自动化错误、系统问题和无缺陷可用于所有项目(个人或内部)。然后,此查询会导致相同的 test_item 并且其结果在所有项目中重复。

然后我查看了一个 db 图,注意到有这些空模板表:pattern_template_test_itempattern_template. 我在弄清楚如何使用这些方面没有任何成功。我看到该project表与 有关系,pattern_template并且pattern_templatepattern_template_test_item有关系test_item

任何人都知道如何编写这个sql?我的最终目标是从查询中获得不重复的输出。另外,我不能在这里使用 distinct 因为我需要测试和结果所属的实际项目。

在此处输入图像描述

4

1 回答 1

1

这只是从test_item到的几个launch连接project

SELECT ti.*
     , pr.name
  FROM test_item AS ti
  JOIN launch    AS la
    ON ti.launch_id = la.id
  JOIN project   AS pr
    ON la.project_id = pr.id
;

只需根据需要扩展您当前的 SQL 以添加这些额外的连接。

但是请注意,您LEFT JOIN在加入时使用了 a test_item

test_item这表明您可能不会在每一行中都有匹配项。您需要决定在这些情况下要做什么。如果您想使用空值而不是项目名称来保留这些结果,请使用外连接。

像这样:

SELECT ti.*
     , COALESCE(pr.name, 'NoProject') AS proj_name
  FROM test_item AS ti
  LEFT JOIN launch    AS la
    ON ti.launch_id = la.id
  LEFT JOIN project   AS pr
    ON la.project_id = pr.id
;
于 2021-09-01T22:52:45.313 回答