Suppose this :
Table A (Batches)
--------------------
Columns :
BatchID
...
Table B (Emails)
------------------
Columns :
EmailID
BatchID
SentStatus
...
I'm writing a program (argument's sake) where a user uploads a batch of emails to a server. The server then sends the batch, allowing the user to continue to work. A batch is created to keep track of who uploaded the batch, when it was uploaded etc. Emails are linked to the Batches by the BatchID column. The SentStatus column in the Emails table is either 0 (Pending), 1 (Sent) or 2 (Error).
I need a query that will return the progress of a batch. Basically, this is the number of Emails that have been sent (or had an error) divided by the number of Emails that are Pending.
I've tried
Code: Select all
SELECT BatchID, BatchDate,
(((SELECT COUNT(*) FROM Emails WHERE BatchStatus = 1) +
(SELECT COUNT(*) FROM Emails WHERE BatchStatus = 2))/
(SELECT COUNT(*) FROM Emails WHERE BatchStatus = 3)) as
'Progress'
FROM Batches INNER JOIN EMAILS ON Emails.BatchID = Batches.BatchID
EDIT : "correct" meaning either 0% or 100%, and not in between.