Question

Given the following query:

    SELECT dbo.ClientSub.chk_Name as Details, COUNT(*) AS Counts
    FROM dbo.ClientMain 
      INNER JOIN 
        dbo.ClientSub 
          ON dbo.ClientMain.ate_Id = dbo.ClientSub.ate_Id 
    WHERE chk_Status=1 
    GROUP BY dbo.ClientSub.chk_Name

I want to display the rows in the aggregation even if there are filtered in the WHERE clause.

Was it helpful?

Solution

NULL values are considered as zeros for aggregation purposes.

For your intention you should use GROUP BY ALL that returns rows which were filtered also, with zero as aggregated value.

OTHER TIPS

in case you use oracle sql:

Have you tried Select nvl(dbo.ClientSub.chk_name,'0') .... ?

I hope you are using Oracle. You can directly use count(chk_name). Passing a column name in the count neglects null values. Your query above returns the total number of records against each chk_name group. When you use count(chk_name) it will count all the records where chk_name is not null.

I hope that answers your question.

Thanks,

Aditya

Do you mean this? Which returns count 0 if chk_Name is NULL.

SELECT 
    dbo.ClientSub.chk_Name as Details, 
    SUM(CASE WHEN ISNULL(dbo.ClientSub.chk_Name, '')<>'' then 1 else 0 end) AS Counts
FROM 
    dbo.ClientMain INNER JOIN dbo.ClientSub ON dbo.ClientMain.ate_Id = dbo.ClientSub.ate_Id 
where chk_Status=1      
group by dbo.ClientSub.chk_Name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top