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.
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.
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
add a comment |
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.
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.
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
in what way did you try changing theJOIN
? by changing the tables beingJOIN
ed? by changing theJOIN
conditions? by trying various flavors ofOUTER 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
add a comment |
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.
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.
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
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.
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.
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
sql ms-access
edited Nov 15 at 23:16
asked Nov 10 at 0:54
soundman87
82
82
in what way did you try changing theJOIN
? by changing the tables beingJOIN
ed? by changing theJOIN
conditions? by trying various flavors ofOUTER 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
add a comment |
in what way did you try changing theJOIN
? by changing the tables beingJOIN
ed? by changing theJOIN
conditions? by trying various flavors ofOUTER 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 JOIN
ed? 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 JOIN
ed? 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
add a comment |
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;
I tried this and it didn't work for me.
– soundman87
Nov 15 at 23:17
add a comment |
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
add a comment |
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;
I tried this and it didn't work for me.
– soundman87
Nov 15 at 23:17
add a comment |
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;
I tried this and it didn't work for me.
– soundman87
Nov 15 at 23:17
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 13 at 5:25
soundman87
82
82
answered Nov 10 at 12:57
Gordon Linoff
748k34285391
748k34285391
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
in what way did you try changing the
JOIN
? by changing the tables beingJOIN
ed? by changing theJOIN
conditions? by trying various flavors ofOUTER 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