I have two SQL tables : TableA and Table B
TableA
| batch_identifier (varchar) | batch_status (bit) | dt_utc (datetime) | batch_tasks |
|---|---|---|---|
| batch_id1 | 0 | 2022-09-15 10:00:00 | 1 |
| batch_id1 | 1 | 2022-09-15 11:00:00 | 1 |
| batch_id2 | 0 | 2022-09-15 10:30:00 | 4 |
TableB
| batch_identifier (varchar) | task_status(bit) | task_description(varchar) | task_identifier(varchar) |
|---|---|---|---|
| batch_id1 | 0 | 2022-09-15 10:00:00 | task_id1 |
| batch_id1 | 1 | 2022-09-15 10:50:00 | task_id1 |
| batch_id2 | 0 | 2022-09-15 10:30:00 | task_id1 |
| batch_id2 | 1 | 2022-09-15 10:35:00 | task_id1 |
| batch_id2 | 0 | 2022-09-15 10:36:00 | task_id2 |
| batch_id2 | 0 | 2022-09-15 10:37:00 | task_id3 |
What I'm trying to do is the following:
Get a table of 'in progress' batches (i.e batch_status = 0) along with the tasks that have been performed. So for instance in the example above the only batch still running is batch_id2 and the associated completed task is task_id1.
So far I've tested this without success:
select
*
from
(
select
batch_identifier,
MAX(CAST(batch_status as int)) as _status
from
dbo.TableA
group by
batch_identifier
) data
where _status = 0 ??
Can you please help with that?
batch_identifier and task_identifier are SQL index.