I'm working on an SQL query for Access, and have come across an unusual problem. Basically, simplifying names to keep it more generic, the table I'm creating will look like this:
Name |A1Count|A2Count|A3Count
------------------------------
GroupA| 0 | 3 | 4 |
GroupB| 2 | 0 | 1 |
GroupC| 0 | 0 | 0 |
...
and so on. I don't know if I need to go into detail on the actual SQL, but basically I have a fairly generic query, which sums up the counts from a more complex sub query:
Select G.Name, SUM(sqA1Count),...
FROM Group as G, (subquery) AS sq...
The subquery uses a Union as the A1 comes from a seperate table than A2 and A3, and so that I don't leave out counts with all 0's, I use a LEFT JOIN operator in it.
Everything works fine when I'm doing the full table, but I'm going to want to implement a form so that the user has the option of filtering the table according to count=0 or count>0 for either of the three activities. So depending on what options are selected, at the end of the generic query (outside the sub), I will be adding stuff like:
"WHERE A1Count = 0"
or
WHERE A1Count > 0 AND A3Count > 0"
and so on.
My problem is, when I try the first option, it works, but it eliminates all >1's counts from A2 and A3. So the results are:
0-6-1
0-7-4
0-1-2...
If I try it >0, then it gets all A1's above 0, but does the opossite and eliminates A2's and A3's at >0. It does the same thing if I try it with A2 or A3 as the WHERE. Going by that logic, if I try the 2nd example, I just don't get any results. This completely defeats the purpose of why I had a left join.
So can anyone help me out? Should I post the query?
Name |A1Count|A2Count|A3Count
------------------------------
GroupA| 0 | 3 | 4 |
GroupB| 2 | 0 | 1 |
GroupC| 0 | 0 | 0 |
...
and so on. I don't know if I need to go into detail on the actual SQL, but basically I have a fairly generic query, which sums up the counts from a more complex sub query:
Select G.Name, SUM(sqA1Count),...
FROM Group as G, (subquery) AS sq...
The subquery uses a Union as the A1 comes from a seperate table than A2 and A3, and so that I don't leave out counts with all 0's, I use a LEFT JOIN operator in it.
Everything works fine when I'm doing the full table, but I'm going to want to implement a form so that the user has the option of filtering the table according to count=0 or count>0 for either of the three activities. So depending on what options are selected, at the end of the generic query (outside the sub), I will be adding stuff like:
"WHERE A1Count = 0"
or
WHERE A1Count > 0 AND A3Count > 0"
and so on.
My problem is, when I try the first option, it works, but it eliminates all >1's counts from A2 and A3. So the results are:
0-6-1
0-7-4
0-1-2...
If I try it >0, then it gets all A1's above 0, but does the opossite and eliminates A2's and A3's at >0. It does the same thing if I try it with A2 or A3 as the WHERE. Going by that logic, if I try the 2nd example, I just don't get any results. This completely defeats the purpose of why I had a left join.
So can anyone help me out? Should I post the query?