I think you had better give some example
data. I would think Steve's excellent solution
would work if data were like:
DateInscription StudentID ProgMDiv ProgBTh
9/1/2004 A -1 -1
9/1/2004 B -1 0
9/1/2004 C 0 -1
9/1/2004 D -1 -1
9/1/2004 E -1 0
so
SELECT
tblInscriptions.DateInscription,
Sum(Abs([ProgMDiv])) AS TotalProgMDiv,
Sum(Abs([ProgBTh])) AS TotalProgBth,
Sum([ProgMDiv]*[ProgBTh]) AS TotalProgBoth
FROM tblInscriptions
GROUP BY tblInscriptions.DateInscription;
would give (correctly):
DateInscription TotalProgMDiv TotalProgBth TotalProgBoth
9/1/2004 4 3 2
but it sounds like maybe it must be more like:
DateInscription StudentID ProgMDiv ProgBTh
9/1/2004 A -1 0
9/1/2004 A 0 -1
9/1/2004 B -1 0
9/1/2004 C 0 -1
9/1/2004 D -1 0
9/1/2004 D 0 -1
9/1/2004 E -1 0
where the above query would give ("wrongly"):
DateInscription TotalProgMDiv TotalProgBth TotalProgBoth
9/1/2004 4 3 0
If this is the case, one method would be to create a
prequery that groups on date and studentid to give
form of first table:
"Q1": (you might want to give it a better name)
SELECT
tblInscriptions.DateInscription,
tblInscriptions.StudentID,
Min(tblInscriptions.ProgMDiv) AS bProgMDiv,
Min(tblInscriptions.ProgBTh) AS bProgBTh
FROM tblInscriptions
GROUP BY
tblInscriptions.DateInscription,
tblInscriptions.StudentID;
giving:
DateInscription StudentID bProgMDiv bProgBTh
9/1/2004 A -1 -1
9/1/2004 B -1 0
9/1/2004 C 0 -1
9/1/2004 D -1 -1
9/1/2004 E -1 0
Then you can use Steve's method above
SELECT
Q1.DateInscription,
Sum(Abs([bProgMDiv])) AS TotalProgMDiv,
Sum(Abs([bProgBTh])) AS TotalProgBth,
Sum([bProgMDiv]*[bProgBTh]) AS TotalProgBoth
FROM Q1
GROUP BY Q1.DateInscription;
giving
DateInscription TotalProgMDiv TotalProgBth TotalProgBoth
9/1/2004 4 3 2
Of course I could be wrong.
And apologies again for butting in.
Gary Walter
Either I am missing your meaning, or the suggestion I made will give the
right answer i.e. 2.
Telesphore wrote:
Thank you, but the problem is that we need to know how many students
took
only the two programs.
We know that 22 students took ProgBTh and 2 of them took also ProgMDiv
at
the same time. So the answer should be 2.
Thank you Steve,
You didn't miss the meaning, but the following code gives me 0 instead of 2!
SELECT tbInscriptions.DateInscription, Sum([ProgMDiv]*[ProgBTh]) AS
TotalProgBoth
FROM tbInscriptions
GROUP BY tbInscriptions.DateInscription
HAVING (((tbInscriptions.DateInscription)=#9/1/2004#));