需要将两个查询合并为一个

时间:2018-12-19 10:11:58

标签: sql

我正在寻找以下两个查询,它们应该组合成一个唯一的区别

SELECT count("STATUS") AS levl1,
       c.cname,
       ji.created,
       concat(p.pkey, '-', ji.issuenum) AS pkey,
       si.pname,
       ji.summary,
       cu.display_name
FROM "AO_7DEABF_STEP_RESULT" sr,
     "AO_7DEABF_TESTSTEP" ts,
     jiraissue ji,
     project p,
     cwd_user cu,
     nodeassociation na,
     component c,
     issuestatus si
WHERE sr."STATUS" = '4'
  AND sr."STEP_ID" = ts."ID"
  AND ts."ISSUE_ID" = ji.id
  AND ji.id = na.source_node_id
  AND na.sink_node_id = c.id
  AND ji.issuestatus = si.id
GROUP BY ji.summary,
         c.cname,
         ji.created,
         p.pkey,
         ji.issuenum,
         si.pname,
         cu.display_name

SELECT count("STATUS") AS levl2,
       c.cname,
       ji.created,
       concat(p.pkey, '-', ji.issuenum) AS pkey,
       si.pname,
       ji.summary,
       cu.display_name
FROM "AO_7DEABF_STEP_RESULT" sr,
     "AO_7DEABF_TESTSTEP" ts,
     jiraissue ji,
     project p,
     cwd_user cu,
     nodeassociation na,
     component c,
     issuestatus si
WHERE sr."STATUS" = '-1'
  AND sr."STEP_ID" = ts."ID"
  AND ts."ISSUE_ID" = ji.id
  AND ji.id = na.source_node_id
  AND na.sink_node_id = c.id
  AND ji.issuestatus = si.id
GROUP BY ji.summary,
         c.cname,
         ji.created,
         p.pkey,
         ji.issuenum,
         si.pname,
         cu.display_name

我想要下面的结果,

Level1 |Level2 |cname |Created    |PKEY     |Pname  |Summary |Display_name
2      |10     |Test  |2018-12-1  |K-1      |Test   |Test    |Janardhan

1 个答案:

答案 0 :(得分:0)

您可以为此使用条件聚合:

SELECT count(case when sr."STATUS" = '4' then 1 end) as levl1,
       count(case when sr."STATUS" = '-1' then 1 end) AS levl2,
       c.cname,
       ji.created,
       concat(p.pkey, '-', ji.issuenum) AS pkey,
       si.pname,
       ji.summary,
       cu.display_name
FROM "AO_7DEABF_STEP_RESULT" sr,
     "AO_7DEABF_TESTSTEP" ts,
     jiraissue ji,
     project p,
     cwd_user cu,
     nodeassociation na,
     component c,
     issuestatus si
WHERE sr."STEP_ID" = ts."ID"
  AND ts."ISSUE_ID" = ji.id
  AND ji.id = na.source_node_id
  AND na.sink_node_id = c.id
  AND ji.issuestatus = si.id
GROUP BY ji.summary,
         c.cname,
         ji.created,
         p.pkey,
         ji.issuenum,
         si.pname,
         cu.display_name;

诸如count()之类的聚合函数将忽略NULL值。如果状态不具有所需的值,则计数内的case表达式将返回NULL