简体   繁体   English

未找到结果时在SQL查询中输入

[英]Make an entry in SQL query when no result is found

I have a query that counts values for an specific kind of values: 我有一个查询,用于计算特定类型的值的值:

SELECT

 B.BG_USER_05 as Functionality,
 count(case when bg_status= 'New' then 1 end) as 'New',
 count(case when bg_status= 'Open' then 1 end) as 'Open',
 count(case when bg_status= 'Assigned' then 1 end) as 'Assigned',
 count(case when bg_status= 'Fixed' then 1 end) as 'Fixed',
 count(case when bg_status= 'Ready to Test' then 1 end) as 'Ready_to_Test',
 count(case when bg_status= 'Reopen' then 1 end) as 'Reopen',
 count(case when bg_status= 'Closed' then 1 end) as 'Closed',
 count(case when bg_status= 'Rejected' then 1 end) as 'Rejected'

FROM
    BUG B,
    RELEASES R

WHERE
    B.BG_DETECTED_IN_REL = R.REL_ID

GROUP BY
    B.BG_USER_05

The query works correctly and returns something like this: 该查询正常工作,并返回如下所示的内容:

UAT 5   13  2   3   0
SIT 14  82  59  18  8

The issue is that sometimes, there are no UAT elements in the table (which is completely normal), which will make the result something like this: 问题是有时表中没有UAT元素(这是完全正常的),这将使结果如下所示:

SIT 14  82  59  18  8

The problem is that I NEED the fist row to be UAT information, and I need the result to be somethin like this: 问题是我需要第一行作为UAT信息,并且我需要结果像这样:

UAT 0   0   0   0   0
SIT 14  82  59  18  8

I have no idea how to approach this. 我不知道该如何处理。 Any idea? 任何想法?

You can solve this using left outer join : 您可以使用left outer join解决此问题:

SELECT u.Functionality,
       count(case when bg_status= 'New' then 1 end) as "New",
       count(case when bg_status= 'Open' then 1 end) as "Open",
       count(case when bg_status= 'Assigned' then 1 end) as "Assigned",
       count(case when bg_status= 'Fixed' then 1 end) as "Fixed",
       count(case when bg_status= 'Ready to Test' then 1 end) as "Ready_to_Test",
       count(case when bg_status= 'Reopen' then 1 end) as "Reopen",
       count(case when bg_status= 'Closed' then 1 end) as "Closed",
       count(case when bg_status= 'Rejected' then 1 end) as "Rejected"
FROM (SELECT 'UAT' as functionality UNION ALL SELECT 'SIT') as u LEFT OUTER JOIN
     BUG B
     ON u.functionality = B.BG_USER_05 LEFT OUTER JOIN
     RELEASES R
     ON B.BG_DETECTED_IN_REL = R.REL_ID
GROUP BY u.functionality;

I also changed the query to use explicit join syntax (where the conditions are in the on clause). 我还更改了查询以使用显式联接语法(条件在on子句中)。 And, the column aliases use double quotes rather than single quotes. 并且,列别名使用双引号而不是单引号。 Single quotes should only be used for string and date constants; 单引号仅应用于字符串和日期常量。 their use for identifiers often leads to confusion. 使用它们作为标识符通常会导致混乱。

Maybe, something like this: 也许是这样的:

SELECT Functionality, sum('New'), sum('Open'),sum('Assigned'),sum('Fixed'),sum('Ready_to_Test'),sum('Reopen'),sum('Closed'), sum('Rejected') 
from
(
SELECT

B.BG_USER_05 as Functionality,
count(case when bg_status= 'New' then 1 end) as 'New',
count(case when bg_status= 'Open' then 1 end) as 'Open',
count(case when bg_status= 'Assigned' then 1 end) as 'Assigned',
count(case when bg_status= 'Fixed' then 1 end) as 'Fixed',
count(case when bg_status= 'Ready to Test' then 1 end) as 'Ready_to_Test',
count(case when bg_status= 'Reopen' then 1 end) as 'Reopen',
count(case when bg_status= 'Closed' then 1 end) as 'Closed',
count(case when bg_status= 'Rejected' then 1 end) as 'Rejected'

FROM
   BUG B,
   RELEASES R

WHERE
   B.BG_DETECTED_IN_REL = R.REL_ID

GROUP BY
   B.BG_USER_05

UNION

SELECT 'UAT' AS Functionality,
0          AS 'New',
0          AS 'Open',
0          AS 'Assigned',
0          AS 'Fixed',
0          AS 'Ready_to_Test',
0          AS 'Reopen',
0          AS 'Closed',
0          AS 'Rejected'
)

group by Functionality

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM