Question

I'm trying to write an SQL query to show the sum of every category. The problem is that I need the sum of every category and even if the sum is zero it should still display the results.

I am doing this by using the DAL layer, so through C# and I'm using an access 2010 database.

This is my current working query:

SELECT        SUM(f.bedrag) AS totaal, c.omschrijving, Limiet.maximumBedrag
FROM            ((Financien f INNER JOIN
                         Categorie c ON f.categorieId = c.id) INNER JOIN
                         Limiet ON f.limietId = Limiet.id)
WHERE        (f.inkomstOfUitgave = 1)
GROUP BY f.categorieId, c.omschrijving, Limiet.maximumBedrag

Now on top of the SUM function I also need to check for nulls, and if the sum of a category is 0 it should display 0. All results that are 0 now are filtered out, but I HAVE to see them.

Does anybody know how to fix it? I tried IsNull() and NZ but I couldn't get it working. Here is a screenshot of the result of the query.

As you can see I get two results, but I should get 7 if the query would display the sum 0 results to.

Anybody knows how to fix this?

Was it helpful?

Solution 2

The answer for also showing the 0 values was: The answer was made by "drch", and I thank him a lot for this! :)

SUM(IIF(IsNULL(f.bedrag), 0, f.bedrag)) AS bedrag

There where a lot of other things wrong too, this is the full query:

SELECT        c.omschrijving as Omschrijving, SUM(IIF(IsNULL(f.bedrag), 0, f.bedrag)) AS bedrag, l.maximumBedrag as maximumBedrag
FROM            ((Categorie c LEFT OUTER JOIN
                         Financien f ON f.categorieId = c.id) LEFT OUTER JOIN
                         Limiet l ON l.categorieId = c.id)
WHERE        (f.inkomstOfUitgave IS NULL) OR
                         (f.inkomstOfUitgave = 1)
GROUP BY c.id, f.categorieId, c.omschrijving, l.maximumBedrag

OTHER TIPS

If the question is about how to display Null as 0 in an Access query, consider this simple query. It returns some rows with Null in the SumOfbedrag column.

SELECT y.categorieId, Sum(y.bedrag) AS SumOfbedrag
FROM YourTable AS y
GROUP BY y.categorieId

Using that SQL as a subquery, the containing query can transform Null to 0 with an IIf() expression.

SELECT
    sub.categorieId,
    IIf(sub.SumOfbedrag Is Null, 0, sub.SumOfbedrag) AS totaal
FROM
    (
        SELECT y.categorieId, Sum(y.bedrag) AS SumOfbedrag
        FROM YourTable AS y
        GROUP BY y.categorieId
    ) AS sub;

However, looking at your screenshot again makes me think the issue is actually that your query does not include rows with Null in the totaal column. In that case, examine the underlying data before you aggregate it with GROUP BY. See whether this query returns any rows ...

SELECT f.bedrag, c.omschrijving, Limiet.maximumBedrag
FROM
    (Financien f
    INNER JOIN Categorie c
    ON f.categorieId = c.id)
    INNER JOIN Limiet
    ON f.limietId = Limiet.id
WHERE
        f.inkomstOfUitgave = 1
    AND f.bedrag Is Null

If that query returns no rows, my best guess is that one or both of your joins should be changed from INNER to LEFT JOIN or RIGHT JOIN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top