SQL在计数中显示0

时间:2015-09-03 13:33:57

标签: sql

我写了下面的查询。但是我无法在相应的计数中得到0。你能告诉我怎样才能加入这个查询来显示0?

SELECT  b.collected     AS Last_Week_Collected,    
        a.collected     AS THIS_Week_Collected,   
        b.errored       AS Last_Week_Errored,   
        a.errored       AS THIS_Week_Errored,   
        b.processed     AS Last_Week_Processed,   
        a.processed     AS THIS_Week_Processed    
FROM    (
            SELECT stream_id,collected, errored, processed 
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY')= '03-09-2015'  
        ) a ,  
        (
            SELECT stream_id,collected,errored ,processed  
            FROM processing_Stats_Archive  
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5' 
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY')= '27-08-2015'  
        ) b   
WHERE   a.stream_id=b.stream_id;

4 个答案:

答案 0 :(得分:2)

查看语法,看起来您正在使用Oracle数据库。所以,NVL函数应该对你有用。此外,由于您希望返回0代替空值,而不是inner join,您可能希望执行某种形式的outer join(左,右或完全取决于根据你的需要)。如果要返回两个查询中的所有行,则需要使用FULL OUTER JOIN,而不是这样:

SELECT nvl(b.collected, 0) AS Last_Week_Collected
    ,nvl(a.collected, 0) AS THIS_Week_Collected
    ,nvl(b.errored, 0) AS Last_Week_Errored
    ,nvl(a.errored, 0) AS THIS_Week_Errored
    ,nvl(b.processed, 0) AS Last_Week_Processed
    ,nvl(a.processed, 0) AS THIS_Week_Processed
FROM (
    SELECT stream_id
        ,collected
        ,errored
        ,processed
    FROM processing_Stats_Archive
    WHERE stream_id = '29'
        AND HR_OF_DAY = '5'
        AND TO_CHAR(batch_Creation_date, 'DD-MON-YY') = '03-09-2015'
    ) a
FULL OUTER JOIN (
    SELECT stream_id
        ,collected
        ,errored
        ,processed
    FROM processing_Stats_Archive
    WHERE stream_id = '29'
        AND HR_OF_DAY = '5'
        AND TO_CHAR(batch_Creation_date, 'DD-MON-YY') = '27-08-2015'
    ) b ON a.stream_id = b.stream_id;

答案 1 :(得分:1)

您可以使用条件聚合:

SELECT  NVL(Last_Week_Collected, 0) AS Last_Week_Collected,
        NVL(THIS_Week_Collected, 0) AS THIS_Week_Collected,
        NVL(Last_Week_Errored, 0) AS Last_Week_Errored,
        NVL(THIS_Week_Errored, 0) AS THIS_Week_Errored,
        NVL(Last_Week_Processed, 0) AS Last_Week_Processed,
        NVL(THIS_Week_Processed, 0) AS THIS_Week_Processed
FROM    
(   SELECT  MAX(CASE WHEN TO_CHAR(batch_Creation_date,'DD-MON-YY')= '27-08-2015' THEN collected ELSE 0 END) AS Last_Week_Collected,    
            MAX(CASE WHEN TO_CHAR(batch_Creation_date,'DD-MON-YY')= '03-09-2015' THEN collected ELSE 0 END) AS THIS_Week_Collected,   
            MAX(CASE WHEN TO_CHAR(batch_Creation_date,'DD-MON-YY')= '27-08-2015' THEN errored ELSE 0 END) AS Last_Week_Errored,   
            MAX(CASE WHEN TO_CHAR(batch_Creation_date,'DD-MON-YY')= '03-09-2015' THEN errored ELSE 0 END) AS THIS_Week_Errored,   
            MAX(CASE WHEN TO_CHAR(batch_Creation_date,'DD-MON-YY')= '27-08-2015' THEN processed ELSE 0 END) AS Last_Week_Processed,   
            MAX(CASE WHEN TO_CHAR(batch_Creation_date,'DD-MON-YY')= '03-09-2015' THEN processed ELSE 0 END) AS THIS_Week_Processed   
    FROM    processing_Stats_Archive 
    WHERE   stream_id = '29'  
    AND     HR_OF_DAY ='5'   
    AND     TO_CHAR(batch_Creation_date,'DD-MON-YY') IN ('27-08-2015', '03-09-2015')
) t;

您当前的查询只有在每周有一行时才有效,所以我认为是这种情况。因此,虽然我已经应用了MAX函数,但它没有意义,因为它是一行的MAX

这是一个标量聚合,也就是说它有一个聚合函数而没有group by,无论是否有数据,它总会返回一行。

答案 2 :(得分:0)

可能是你想要这个输出

   SELECT COALESCE(b.collected,0) AS Last_Week_Collected
,a.collected AS THIS_Week_Collected
,COALESCE(b.errored,0) AS   Last_Week_Errored   
,a.errored AS THIS_Week_Errored   
,COALESCE(b.processed,0) AS Last_Week_Processed
,a.processed AS   THIS_Week_Processed 
FROM   
(
SELECT stream_id,collected,errored ,processed 
FROM processing_Stats_Archive
WHERE stream_id = '29'  AND HR_OF_DAY ='5' 
AND TO_CHAR(batch_Creation_date,'DD-MON-YY')= '03-09-2015'  
)a LEFT JOIN 
(
SELECT stream_id,collected,errored ,processed  
FROM processing_Stats_Archive 
WHERE stream_id = '29'  AND HR_OF_DAY ='5' AND TO_CHAR(batch_Creation_date,'DD-MON-YY')= '27-08-2015'  
) 
b ON a.stream_id=b.stream_id

答案 3 :(得分:0)

这个想法是没有任何类型的JOIN或聚合函数(如MAX)可以帮助你,如果没有什么可以加入或聚合。您应该使用单独的查询来检索单个“已收集”,“错误”和“已处理”列,并将它们合并为0.我在此处使用NVL,认为您正在使用Oracle DBMS

试试这个:

SELECT (
        NVL(SELECT collected
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY') =  '03-09-2015' ,0)
        AS THIS_Week_Errored, 
            NVL(SELECT errored
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY') =  '03-09-2015' ,0) 
        AS THIS_Week_Errored,
            NVL (SELECT processed
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY') =  '03-09-2015' ,0) 
        AS THIS_Week_Processed ) 
        UNION
        NVL(SELECT collected
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY') =  '27-08-2015' ,0)
        AS Last_Week_Collected, 
            NVL(SELECT errored
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY') =  '27-08-2015' ,0) 
        AS Last_Week_Errored,
            NVL (SELECT processed
            FROM processing_Stats_Archive 
            WHERE stream_id = '29'  
            AND HR_OF_DAY ='5'  
            AND TO_CHAR(batch_Creation_date,'DD-MON-YY') =  '27-08-2015' ,0) 
        AS Last_Week_Processed )
    FROM DUAL