I'm new to mySQL and working on a query which essentially takes one initial table and summarizes its contents in two separate tables. The second "summary" table is dropping tuples where a value is 0; I need to fix that. Here's the specifics:
I have a large Inventory table, which I assemble from other tables:
SID CID SECTION RATING
==========================
100 1001 1 A
101 1001 1 A
102 1002 1 B
103 1002 2 F
104 1003 1 A
...etc...
("CID" and "Section" are the primary keys here, I don't know if whether the PK is spread across two attributes is significant here.) I have a successful query ("numTable1") which counts the number of SIDs for each CID and Section. For example, with the above Inventory table, "sumTable1" would be:
CID SECTION numSID
=====================
1001 1 2
1002 1 1
1002 2 1
1003 1 1
...etc...
Here's the SQL:
create temporary table numTable1(
SELECT ot1.cid, ot2.section, count(ot1.sid) 'numSID'
FROM otherTable1 ot1, otherTable2 ot2
WHERE ot1.cid=ot2.cid and ot1.section=ot2.section
GROUP BY ot1.cid, ot2.section
);
"NumTable" works great. Here's the problem: I also need a query which counts the number of 'A' Ratings per CID/Section:
create temporary table numA(
select t9.cid, t9.section, count(ot1.rating) 'As'
from otherTable1 ot1, t9
where grade = 'A' and t9.cid=ot1.cid and t9.section=ot1.section
group by t9.cid, t9.section
);
Here's the output of "numA":
CID SECTION As
=====================
1001 1 2
1003 1 1
...etc...
But here's the problem; some CID/Sections have no A Ratings, and they are being washed out. What I need is this:
CID SECTION As
=====================
1001 1 2
1002 1 0
1002 2 0
1003 1 1
...etc...
You'll note that the above table can be seamlessly merged with "numTable1", which is the ultimate aim here:
CID SECTION numSID As
==========================
1001 1 2 2
1002 1 1 0
1002 2 1 0
1003 1 1 1
But as long as "numA" drops CID/Sections with 0 As, I'm dead in the water. I assume my "group by" statement in "numA" is washing out the zeroes... but I'm not sure about that. Any suggestions?