Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: How to work around count discarding null rows?
Hi, Matt,
At first lets check how I've understood your problem, Ok?
Well, you have a table of persons, for instance let call it STUDENT.
Each person at the table is characterised with three attributes
(Grade, Major and Ethnicity).
You need to perform a query which returns ALL POSSIBLE
combination of Grade, Major and Ethnicity and count the total
quantity of persons that fit (match?) each combination.
Is it correct description of your task?
The FIRST POINT is to obtain a Decart Product of all grades, majors and
ethnicities.
Then for each combination to calculate a quantity of person that match
the grade+major+ethnicity
For eg., you can perform it with the following query:
Example 1.
SELECT G.Grade, M.Major, E.Ethnicity,
(SELECT Count(*) FROM STUDENT S WHERE
S.Grade = G.Grade AND S.Major = M.Major AND S.Ethnicity = E.Ethnicity)
"Count"
FROM
(SELECT DISTINCT Grade FROM STUDENT WHERE Grade IS NOT NULL) G
(SELECT DISTINCT Major FROM STUDENT WHERE Major IS NOT NULL) M
(SELECT DISTINCT Ethnicity FROM STUDENT WHERE Ethnicity IS NOT NULL) E
Usually developers design separate tables for all grades as well as majors
and ethnicities :-)))
something like the following:
CREATE TABLE GRADES(
Id Number -- unique Id of grade
Name VarChar2(40) -- name or title
);
ALTER TABLE GRADES ADD Primary Key (Id)
Let for instance that others table have a similar structure.
In that case it's possible to transform Example 1 to...
Example 2
Bye.
Regards,
Segrey Balter
balter_at_kompas.donetsk.ua
P. S. Sorry for pure English.
Don't hesitate to ask me if something is still foggy :-)).
Received on Tue Oct 29 2002 - 09:01:40 CST