hi to all - consider a table like the one below:
I have written some SQL to count MATCHES between Val_A and Val_B of the same ID.
When I run the Query with the Parameter = 1, I get the expected query output showing that there
are 2 records having 1 MATCH. (ID=10 and ID=30).
When I run the Query with the Parameter = 2, I get the expected query output showing that there
are NO records having 2 MATCHES.
Here's the problem...
When run the Query with the Parameter = 0, the output shows NO records. But, I want it to show
that there is 1 record (namely ID=20) that has 0 matches. To picture this more clearly, imagine that tbl_Val has records for 1000 different IDs. I'd like results that tell me something like this:
for MATCH COUNT = 3, there are 100 records having 3 matches
for MATCH COUNT = 2, there are 50 records having 3 matches
for MATCH COUNT = 1, there are 250 records having 3 matches
for MATCH COUNT = 0, there are 600 records having 3 matches this is the problem
I think the problem must be related to the fact that if there are NO MATCHES as is the case for ID=20, then the GROUP BY returns no records at all for this ID. I had hoped that NZ would help, but it doesn't seem to.
Is their a 'fix'?
Thanks, Vicky
Code:
[b]
tbl_Val [/b]
ID Pkg Val_A Val_B
10 1 [b]34[/b] 26
10 2 12 [b]34[/b]
10 3 55 92
20 1 10 88
20 2 20 99
20 3 30 77
30 1 12 [b]76[/b]
30 2 99 44
30 3 [b]76[/b] 51
40 etc...
I have written some SQL to count MATCHES between Val_A and Val_B of the same ID.
Code:
SELECT T1.ID, T1.Val_A, T1.Val_B
FROM tbl_Val AS T1 INNER JOIN tbl_Val AS T2 ON (T1.ID = T2.ID) AND (T1.Val_A = T2.Val_B)
GROUP BY T1.ID
HAVING (Count(*)=[Enter MATCH COUNT]);
When I run the Query with the Parameter = 1, I get the expected query output showing that there
are 2 records having 1 MATCH. (ID=10 and ID=30).
When I run the Query with the Parameter = 2, I get the expected query output showing that there
are NO records having 2 MATCHES.
Here's the problem...
When run the Query with the Parameter = 0, the output shows NO records. But, I want it to show
that there is 1 record (namely ID=20) that has 0 matches. To picture this more clearly, imagine that tbl_Val has records for 1000 different IDs. I'd like results that tell me something like this:
for MATCH COUNT = 3, there are 100 records having 3 matches
for MATCH COUNT = 2, there are 50 records having 3 matches
for MATCH COUNT = 1, there are 250 records having 3 matches
for MATCH COUNT = 0, there are 600 records having 3 matches this is the problem
I think the problem must be related to the fact that if there are NO MATCHES as is the case for ID=20, then the GROUP BY returns no records at all for this ID. I had hoped that NZ would help, but it doesn't seem to.
Is their a 'fix'?
Thanks, Vicky