COUNT返回NULL,应该返回0

时间:2013-10-05 18:14:16

标签: oracle

我对表有一个简单的查询:

Person id Organization id  employee_nam age  busines_group_id
123       Zuyo         10     John      30   81
2457      Zuyo         10     Geet      69   81
56        Ghiya        12     paul      20   81 
          frei         13                    81

SELECT
    COUNT(DISTINCT ped.person_id)
FROM
    per_emp_detail ped
WHERE
    ped.business_group_id = 81
    AND
    ped.id = NVL(p_org_id, ped.organization_id);

SELECT
    NVL(COUNT(DISTINCT ped.person_id), 0)
FROM
    per_emp_detail ped
WHERE
    ped.business_group_id = 81
    AND
    ped.id = NVL(p_org_id, ped.organization_id);

p_org_id是我传递的参数,可以是10,12或13。 对于ID为12,COUNT会为ID为2返回1,但对于ID为13,则返回NULL。 我希望在这种情况下返回0

NVLCASE也无效。

1 个答案:

答案 0 :(得分:1)

尝试MAX:

nvl(max(count(DISTINCT ped.person_id)),0)