How to Return a value of Zero for null Value in Count Query in MS-Access?











up vote
0
down vote

favorite












I have two Opening Types that I am trying to retrieve counts for in my query in MS-Access. As seen in the photo below. When either both of those values are present in my results they will produce the quantity of how many position are in for each.



Opening Types



When one of the opening types has not been selected at all on the respected table it does not show on the query count as seen below.



Only 1 category displaying



My SQL is as follows



SELECT tblOpening.fk_OpeningTypeId
,Count(tblOpening.Position) AS CountOfPosition
FROM tblOpeningCity
INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
WHERE (
((tblOpening.Position) = "Flex Officer")
AND ((tblOpening.Closed) = No)
AND (
(tblOpeningCity.OpeningCity) = "Livermore"
OR (tblOpeningCity.OpeningCity) = "Pleasanton"
)
)
GROUP BY tblOpening.fk_OpeningTypeId;


I have tried changing the join and no luck. Any Help would be appreciated.



Here is some sample data that is similar to what my database would use.



https://drive.google.com/open?id=1X1W-wctcP7SiYIOWx4VYTOh03zddF81r










share|improve this question
























  • in what way did you try changing the JOIN? by changing the tables being JOINed? by changing the JOIN conditions? by trying various flavors of OUTER JOIN? please be more specific; also, please set up a SQL Fiddle, or provide schema and data as text so that someone who wants to help can help you readily
    – landru27
    Nov 10 at 1:04










  • I tried different types of the Outer join. I also just added a link to a sample database of some of the information that I have.
    – soundman87
    Nov 13 at 1:14















up vote
0
down vote

favorite












I have two Opening Types that I am trying to retrieve counts for in my query in MS-Access. As seen in the photo below. When either both of those values are present in my results they will produce the quantity of how many position are in for each.



Opening Types



When one of the opening types has not been selected at all on the respected table it does not show on the query count as seen below.



Only 1 category displaying



My SQL is as follows



SELECT tblOpening.fk_OpeningTypeId
,Count(tblOpening.Position) AS CountOfPosition
FROM tblOpeningCity
INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
WHERE (
((tblOpening.Position) = "Flex Officer")
AND ((tblOpening.Closed) = No)
AND (
(tblOpeningCity.OpeningCity) = "Livermore"
OR (tblOpeningCity.OpeningCity) = "Pleasanton"
)
)
GROUP BY tblOpening.fk_OpeningTypeId;


I have tried changing the join and no luck. Any Help would be appreciated.



Here is some sample data that is similar to what my database would use.



https://drive.google.com/open?id=1X1W-wctcP7SiYIOWx4VYTOh03zddF81r










share|improve this question
























  • in what way did you try changing the JOIN? by changing the tables being JOINed? by changing the JOIN conditions? by trying various flavors of OUTER JOIN? please be more specific; also, please set up a SQL Fiddle, or provide schema and data as text so that someone who wants to help can help you readily
    – landru27
    Nov 10 at 1:04










  • I tried different types of the Outer join. I also just added a link to a sample database of some of the information that I have.
    – soundman87
    Nov 13 at 1:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have two Opening Types that I am trying to retrieve counts for in my query in MS-Access. As seen in the photo below. When either both of those values are present in my results they will produce the quantity of how many position are in for each.



Opening Types



When one of the opening types has not been selected at all on the respected table it does not show on the query count as seen below.



Only 1 category displaying



My SQL is as follows



SELECT tblOpening.fk_OpeningTypeId
,Count(tblOpening.Position) AS CountOfPosition
FROM tblOpeningCity
INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
WHERE (
((tblOpening.Position) = "Flex Officer")
AND ((tblOpening.Closed) = No)
AND (
(tblOpeningCity.OpeningCity) = "Livermore"
OR (tblOpeningCity.OpeningCity) = "Pleasanton"
)
)
GROUP BY tblOpening.fk_OpeningTypeId;


I have tried changing the join and no luck. Any Help would be appreciated.



Here is some sample data that is similar to what my database would use.



https://drive.google.com/open?id=1X1W-wctcP7SiYIOWx4VYTOh03zddF81r










share|improve this question















I have two Opening Types that I am trying to retrieve counts for in my query in MS-Access. As seen in the photo below. When either both of those values are present in my results they will produce the quantity of how many position are in for each.



Opening Types



When one of the opening types has not been selected at all on the respected table it does not show on the query count as seen below.



Only 1 category displaying



My SQL is as follows



SELECT tblOpening.fk_OpeningTypeId
,Count(tblOpening.Position) AS CountOfPosition
FROM tblOpeningCity
INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
WHERE (
((tblOpening.Position) = "Flex Officer")
AND ((tblOpening.Closed) = No)
AND (
(tblOpeningCity.OpeningCity) = "Livermore"
OR (tblOpeningCity.OpeningCity) = "Pleasanton"
)
)
GROUP BY tblOpening.fk_OpeningTypeId;


I have tried changing the join and no luck. Any Help would be appreciated.



Here is some sample data that is similar to what my database would use.



https://drive.google.com/open?id=1X1W-wctcP7SiYIOWx4VYTOh03zddF81r







sql ms-access






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 23:16

























asked Nov 10 at 0:54









soundman87

82




82












  • in what way did you try changing the JOIN? by changing the tables being JOINed? by changing the JOIN conditions? by trying various flavors of OUTER JOIN? please be more specific; also, please set up a SQL Fiddle, or provide schema and data as text so that someone who wants to help can help you readily
    – landru27
    Nov 10 at 1:04










  • I tried different types of the Outer join. I also just added a link to a sample database of some of the information that I have.
    – soundman87
    Nov 13 at 1:14


















  • in what way did you try changing the JOIN? by changing the tables being JOINed? by changing the JOIN conditions? by trying various flavors of OUTER JOIN? please be more specific; also, please set up a SQL Fiddle, or provide schema and data as text so that someone who wants to help can help you readily
    – landru27
    Nov 10 at 1:04










  • I tried different types of the Outer join. I also just added a link to a sample database of some of the information that I have.
    – soundman87
    Nov 13 at 1:14
















in what way did you try changing the JOIN? by changing the tables being JOINed? by changing the JOIN conditions? by trying various flavors of OUTER JOIN? please be more specific; also, please set up a SQL Fiddle, or provide schema and data as text so that someone who wants to help can help you readily
– landru27
Nov 10 at 1:04




in what way did you try changing the JOIN? by changing the tables being JOINed? by changing the JOIN conditions? by trying various flavors of OUTER JOIN? please be more specific; also, please set up a SQL Fiddle, or provide schema and data as text so that someone who wants to help can help you readily
– landru27
Nov 10 at 1:04












I tried different types of the Outer join. I also just added a link to a sample database of some of the information that I have.
– soundman87
Nov 13 at 1:14




I tried different types of the Outer join. I also just added a link to a sample database of some of the information that I have.
– soundman87
Nov 13 at 1:14












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Just add your condition with an IIF() statement like that and put a Sum() around it:



SELECT tblOpening.fk_OpeningTypeId
,Sum(IIF(tblOpening.Position = "YourCondition", 1, 0)) AS CountOfPosition
FROM tblOpeningCity
INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
WHERE (
((tblOpening.Position) = "Flex Officer")
AND ((tblOpening.Closed) = No)
AND (
(tblOpeningCity.OpeningCity) = "Livermore"
OR (tblOpeningCity.OpeningCity) = "Pleasanton"
)
)
GROUP BY tblOpening.fk_OpeningTypeId;





share|improve this answer





















  • I tried this and it didn't work for me.
    – soundman87
    Nov 15 at 23:17


















up vote
0
down vote













You can accomplish what you want using a LEFT JOIN. MS Access makes it hard to incorporate the difference conditions, so this probably does what you want:



SELECT o.fk_OpeningTypeId, Count(oc.OpeningCityID) AS CountOfPosition
FROM tblOpening as o LEFT JOIN
(SELECT oc.*
FROM tblOpeningCity oc
WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
) as oc
ON oc.OpeningCityID = o.City
WHERE o.Position = "Flex Officer" AND
o.Closed = No
GROUP BY o.fk_OpeningTypeId;


It is possible that the filters on o are removing what you want. If so, then conditional aggregation will fix that:



SELECT o.fk_OpeningTypeId, 
SUM(IIF(oc.OpeningCityID IS NOT NULL AND
o.Position = "Flex Officer" AND
o.Closed = No, 1, 0)
) AS CountOfPosition
FROM tblOpening as o LEFT JOIN
(SELECT oc.*
FROM tblOpeningCity oc
WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
) as oc
ON oc.OpeningCityID = o.City
GROUP BY o.fk_OpeningTypeId;


Access Database Sample






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235077%2fhow-to-return-a-value-of-zero-for-null-value-in-count-query-in-ms-access%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Just add your condition with an IIF() statement like that and put a Sum() around it:



    SELECT tblOpening.fk_OpeningTypeId
    ,Sum(IIF(tblOpening.Position = "YourCondition", 1, 0)) AS CountOfPosition
    FROM tblOpeningCity
    INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
    WHERE (
    ((tblOpening.Position) = "Flex Officer")
    AND ((tblOpening.Closed) = No)
    AND (
    (tblOpeningCity.OpeningCity) = "Livermore"
    OR (tblOpeningCity.OpeningCity) = "Pleasanton"
    )
    )
    GROUP BY tblOpening.fk_OpeningTypeId;





    share|improve this answer





















    • I tried this and it didn't work for me.
      – soundman87
      Nov 15 at 23:17















    up vote
    0
    down vote













    Just add your condition with an IIF() statement like that and put a Sum() around it:



    SELECT tblOpening.fk_OpeningTypeId
    ,Sum(IIF(tblOpening.Position = "YourCondition", 1, 0)) AS CountOfPosition
    FROM tblOpeningCity
    INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
    WHERE (
    ((tblOpening.Position) = "Flex Officer")
    AND ((tblOpening.Closed) = No)
    AND (
    (tblOpeningCity.OpeningCity) = "Livermore"
    OR (tblOpeningCity.OpeningCity) = "Pleasanton"
    )
    )
    GROUP BY tblOpening.fk_OpeningTypeId;





    share|improve this answer





















    • I tried this and it didn't work for me.
      – soundman87
      Nov 15 at 23:17













    up vote
    0
    down vote










    up vote
    0
    down vote









    Just add your condition with an IIF() statement like that and put a Sum() around it:



    SELECT tblOpening.fk_OpeningTypeId
    ,Sum(IIF(tblOpening.Position = "YourCondition", 1, 0)) AS CountOfPosition
    FROM tblOpeningCity
    INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
    WHERE (
    ((tblOpening.Position) = "Flex Officer")
    AND ((tblOpening.Closed) = No)
    AND (
    (tblOpeningCity.OpeningCity) = "Livermore"
    OR (tblOpeningCity.OpeningCity) = "Pleasanton"
    )
    )
    GROUP BY tblOpening.fk_OpeningTypeId;





    share|improve this answer












    Just add your condition with an IIF() statement like that and put a Sum() around it:



    SELECT tblOpening.fk_OpeningTypeId
    ,Sum(IIF(tblOpening.Position = "YourCondition", 1, 0)) AS CountOfPosition
    FROM tblOpeningCity
    INNER JOIN tblOpening ON tblOpeningCity.OpeningCityID = tblOpening.City
    WHERE (
    ((tblOpening.Position) = "Flex Officer")
    AND ((tblOpening.Closed) = No)
    AND (
    (tblOpeningCity.OpeningCity) = "Livermore"
    OR (tblOpeningCity.OpeningCity) = "Pleasanton"
    )
    )
    GROUP BY tblOpening.fk_OpeningTypeId;






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 12:59









    Strawberryshrub

    9311215




    9311215












    • I tried this and it didn't work for me.
      – soundman87
      Nov 15 at 23:17


















    • I tried this and it didn't work for me.
      – soundman87
      Nov 15 at 23:17
















    I tried this and it didn't work for me.
    – soundman87
    Nov 15 at 23:17




    I tried this and it didn't work for me.
    – soundman87
    Nov 15 at 23:17












    up vote
    0
    down vote













    You can accomplish what you want using a LEFT JOIN. MS Access makes it hard to incorporate the difference conditions, so this probably does what you want:



    SELECT o.fk_OpeningTypeId, Count(oc.OpeningCityID) AS CountOfPosition
    FROM tblOpening as o LEFT JOIN
    (SELECT oc.*
    FROM tblOpeningCity oc
    WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
    ) as oc
    ON oc.OpeningCityID = o.City
    WHERE o.Position = "Flex Officer" AND
    o.Closed = No
    GROUP BY o.fk_OpeningTypeId;


    It is possible that the filters on o are removing what you want. If so, then conditional aggregation will fix that:



    SELECT o.fk_OpeningTypeId, 
    SUM(IIF(oc.OpeningCityID IS NOT NULL AND
    o.Position = "Flex Officer" AND
    o.Closed = No, 1, 0)
    ) AS CountOfPosition
    FROM tblOpening as o LEFT JOIN
    (SELECT oc.*
    FROM tblOpeningCity oc
    WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
    ) as oc
    ON oc.OpeningCityID = o.City
    GROUP BY o.fk_OpeningTypeId;


    Access Database Sample






    share|improve this answer



























      up vote
      0
      down vote













      You can accomplish what you want using a LEFT JOIN. MS Access makes it hard to incorporate the difference conditions, so this probably does what you want:



      SELECT o.fk_OpeningTypeId, Count(oc.OpeningCityID) AS CountOfPosition
      FROM tblOpening as o LEFT JOIN
      (SELECT oc.*
      FROM tblOpeningCity oc
      WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
      ) as oc
      ON oc.OpeningCityID = o.City
      WHERE o.Position = "Flex Officer" AND
      o.Closed = No
      GROUP BY o.fk_OpeningTypeId;


      It is possible that the filters on o are removing what you want. If so, then conditional aggregation will fix that:



      SELECT o.fk_OpeningTypeId, 
      SUM(IIF(oc.OpeningCityID IS NOT NULL AND
      o.Position = "Flex Officer" AND
      o.Closed = No, 1, 0)
      ) AS CountOfPosition
      FROM tblOpening as o LEFT JOIN
      (SELECT oc.*
      FROM tblOpeningCity oc
      WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
      ) as oc
      ON oc.OpeningCityID = o.City
      GROUP BY o.fk_OpeningTypeId;


      Access Database Sample






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can accomplish what you want using a LEFT JOIN. MS Access makes it hard to incorporate the difference conditions, so this probably does what you want:



        SELECT o.fk_OpeningTypeId, Count(oc.OpeningCityID) AS CountOfPosition
        FROM tblOpening as o LEFT JOIN
        (SELECT oc.*
        FROM tblOpeningCity oc
        WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
        ) as oc
        ON oc.OpeningCityID = o.City
        WHERE o.Position = "Flex Officer" AND
        o.Closed = No
        GROUP BY o.fk_OpeningTypeId;


        It is possible that the filters on o are removing what you want. If so, then conditional aggregation will fix that:



        SELECT o.fk_OpeningTypeId, 
        SUM(IIF(oc.OpeningCityID IS NOT NULL AND
        o.Position = "Flex Officer" AND
        o.Closed = No, 1, 0)
        ) AS CountOfPosition
        FROM tblOpening as o LEFT JOIN
        (SELECT oc.*
        FROM tblOpeningCity oc
        WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
        ) as oc
        ON oc.OpeningCityID = o.City
        GROUP BY o.fk_OpeningTypeId;


        Access Database Sample






        share|improve this answer














        You can accomplish what you want using a LEFT JOIN. MS Access makes it hard to incorporate the difference conditions, so this probably does what you want:



        SELECT o.fk_OpeningTypeId, Count(oc.OpeningCityID) AS CountOfPosition
        FROM tblOpening as o LEFT JOIN
        (SELECT oc.*
        FROM tblOpeningCity oc
        WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
        ) as oc
        ON oc.OpeningCityID = o.City
        WHERE o.Position = "Flex Officer" AND
        o.Closed = No
        GROUP BY o.fk_OpeningTypeId;


        It is possible that the filters on o are removing what you want. If so, then conditional aggregation will fix that:



        SELECT o.fk_OpeningTypeId, 
        SUM(IIF(oc.OpeningCityID IS NOT NULL AND
        o.Position = "Flex Officer" AND
        o.Closed = No, 1, 0)
        ) AS CountOfPosition
        FROM tblOpening as o LEFT JOIN
        (SELECT oc.*
        FROM tblOpeningCity oc
        WHERE oc.OpeningCity IN ("Livermore", "Pleasanton")
        ) as oc
        ON oc.OpeningCityID = o.City
        GROUP BY o.fk_OpeningTypeId;


        Access Database Sample







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 at 5:25









        soundman87

        82




        82










        answered Nov 10 at 12:57









        Gordon Linoff

        748k34285391




        748k34285391






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235077%2fhow-to-return-a-value-of-zero-for-null-value-in-count-query-in-ms-access%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff