This is the DB I'm looking at. (Many tables and columns excluded for
simplicity.)
TASK {taskid}
PROCESSTASK {runid, taskid, processid, startstamp, stopstamp}
I have a number of tables representing tasks that can be run multiple
times. Tasks are made up of one or more processes. The table I am
querying on records the start and stop times of the processes. The
purpose is to get the count of currently running processes.
select sc.scriptid, nvl((select count(pt.runid)
from processtask pt
where pt.runid = <RUN_ID>
and pt.stopstamp is null
group by pt.taskid), 0)
from task tk where tk.taskid in (<TASK_IDS>)
Thomas Kellerer - 14 Aug 2007 17:30 GMT
James Appleby wrote on 14.08.2007 17:57:
> This is the DB I'm looking at. (Many tables and columns excluded for
> simplicity.)
[quoted text clipped - 13 lines]
> group by pt.taskid), 0)
> from task tk where tk.taskid in (<TASK_IDS>)
You beat me with that ;)
Actually I tried to run this with Derby, and it seems it does not support the
SELECT inside the column list anyway, so you probably need to rewrite completely.
You don't have any correlation between processtask in the from clause and the
calculation of the number of runids. Is that intended?
So, I'm not sure I understood your query completely (the table for the alias sc
is also missing), but maybe something like this:
SELECT tk.*,
c.num_procs
FROM task tk,
(SELECT pt.taskid AS taskid,
COUNT(pt.runid) AS num_procs
FROM processtask pt
WHERE pt.runid = 1
GROUP BY pt.taskid) c
WHERE c.taskid = tk.taskid
would do the job as well (no need for CASE or nvl whatsoever). But as I said, I
don't think I understood your query completely
Regards
Thomas