I had a hard time coming up with a title for this question, but I think it may be easy for someone who is an SQL guru.
Tables:
SIMULATION: ID, SIMULATION_SET_ID, EXECUTION_STATUS
SIMULATION_SET: SET_ID
SIMULATION_BATCH: ID
BATCH_ELEMENT: SIMULATION_BATCH_ID, SIMULATION_SET_ID
(In words, a set contains a bunch of simulations. A batch contains a bunch of sets via an intermediate table. The Batch is really a meta-container to group sets based on when they are requested, and are used as a container, but don’t themselves contain a status.)
I am trying to return a count of completed batches, and a completed batch would be one in which all of the simulations have an execution status of COMPLETED. However, I seem to be getting any batch that any completed simulation, even if they all aren’t. This was my attempt:
SELECT COUNT(DISTINCT B.ID)
FROM SIMULATION_BATCH B
INNER JOIN BATCH_ELEMENT BE ON BE.SIMULATION_BATCH_ID=B.ID
INNER JOIN SIMULATION S ON S.SIMULATION_SET_ID=BE.SIMULATION_SET_ID
WHERE S.EXECUTION_STATUS ='COMPLETED'";
So after looking back I see this doesn’t work. I tried to add in a AND NOT EXISTS(SELECT…) clause to try to rule out batches that had simulations where the execution status was not completed, but that didn’t work at all, it consistently returned an empty set when anything was running. This is what I added:
AND NOT EXISTS (SELECT SIM.ID
FROM SIMULATION SIM
INNER JOIN BATCH_ELEMENT BE2 ON BE2.SIMULATION_SET_ID=SIM.SIMULATION_SET_ID
WHERE BE.SIMULATION_BATCH_ID=B.ID AND SIM.EXECUTION_STATUS != 'COMPLETED' );
Thanks for any insight into this. I think I need to better learn subqueries but I’m not sure. After I figure out how to get a count, I need to get a list of the batches with a bunch of info about them, but I think I can do that if I can get a count.
Update: I’ve been working on this all day, haven’t made that much progress except more wrong ones. I was thinking an easier way to describe what I’m looking for is I want Batches where ALL Simulations in the batch meet some criteria (like status is omplete). I can do this when ANY (at least 1) Simulation meets the criteria, but it’s the ALL that seems to be throwing me. Any ideas would be greatly appreciated.
I think I see a typo/bug in the subquery that explains why the overall query “consistently returned an empty set when anything was running.”
See this line of your subquery:
I think you meant to have this instead:
If that fixes the problem, then you could also try simplifying the query with something like this:
This assumes that:
It looks like the first case is true since you wrote, “The batch_element record is inserted when the batch is created.” But I’m not sure about the second one.
Good luck!