簡體   English   中英

未找到結果時在SQL查詢中輸入

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

我有一個查詢,用於計算特定類型的值的值:

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

該查詢正常工作,並返回如下所示的內容:

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

問題是有時表中沒有UAT元素(這是完全正常的),這將使結果如下所示:

SIT 14  82  59  18  8

問題是我需要第一行作為UAT信息,並且我需要結果像這樣:

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

我不知道該如何處理。 任何想法?

您可以使用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;

我還更改了查詢以使用顯式聯接語法(條件在on子句中)。 並且,列別名使用雙引號而不是單引號。 單引號僅應用於字符串和日期常量。 使用它們作為標識符通常會導致混亂。

也許是這樣的:

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