简体   繁体   English

SQL 服务器加入子查询

[英]SQL Server join on subquery

I have two SQL tables: TableA and Table B我有两个 SQL 表:表 A 和表 B

TableA表A

batch_identifier (varchar)批处理标识符(varchar) batch_status (bit)批处理状态(位) dt_utc (datetime) dt_utc(日期时间) batch_tasks批处理任务
batch_id1 batch_id1 0 0 2022-09-15 10:00:00 2022-09-15 10:00:00 1 1
batch_id1 batch_id1 1 1 2022-09-15 11:00:00 2022-09-15 11:00:00 1 1
batch_id2 batch_id2 0 0 2022-09-15 10:30:00 2022-09-15 10:30:00 4 4

TableB表B

batch_identifier (varchar)批处理标识符(varchar) task_status(bit)任务状态(位) task_description(varchar)任务描述(varchar) task_identifier(varchar)任务标识符(varchar)
batch_id1 batch_id1 0 0 2022-09-15 10:00:00 2022-09-15 10:00:00 task_id1 task_id1
batch_id1 batch_id1 1 1 2022-09-15 10:50:00 2022-09-15 10:50:00 task_id1 task_id1
batch_id2 batch_id2 0 0 2022-09-15 10:30:00 2022-09-15 10:30:00 task_id1 task_id1
batch_id2 batch_id2 1 1 2022-09-15 10:35:00 2022-09-15 10:35:00 task_id1 task_id1
batch_id2 batch_id2 0 0 2022-09-15 10:36:00 2022-09-15 10:36:00 task_id2 task_id2
batch_id2 batch_id2 0 0 2022-09-15 10:37:00 2022-09-15 10:37:00 task_id3 task_id3

What I'm trying to do is the following:我想要做的是以下几点:
Get a table of 'in progress' batches (ie batch_status = 0) along with the tasks that have been performed.获取“进行中”批处理表(即 batch_status = 0)以及已执行的任务。 So for instance in the example above the only batch still running is batch_id2 and the associated completed task is task_id1.因此,例如在上面的示例中,唯一仍在运行的批处理是 batch_id2,而相关的已完成任务是 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. batch_identifiertask_identifier是 SQL 索引。

Maybe something like one of these?也许像其中之一?

SELECT batch_identifier, db_utc, Zero, One
FROM TableA
    LEFT JOIN (
        SELECT batch_identifier,
            SUM(CASE WHEN task_status = 0 THEN 1 ELSE 0 END) AS Zero,
            SUM(CASE WHEN task_status = 1 THEN 1 ELSE 0 END) AS One
        FROM TableB
        GROUP BY batch_identifier
    ) AS b
        ON a.batch_identifier = b.batch_identifier

SELECT *
FROM TableA a
WHERE NOT EXISTS (
    SELECT NULL FROM TableB b WHERE b.batch_identifier = a.batch_identifier AND b.task_status = 0
)

If I'm understanding you correctly, I don't think you need a subquery.如果我对您的理解正确,我认为您不需要子查询。

Try:尝试:

SELECT batch_identifier, task_identifier, MAX(CAST(batch_status as int)) as _status
FROM dbo.TableA a
INNER JOIN dbo.TableB b ON a.batch_identifier = b.batch_identifier AND task_status = 1
WHERE batch_status = 0
GROUP BY batch_identifier, task_identifier

Using common table expression to get what batch(es) are still in progress.使用公用表表达式来获取哪些批次仍在进行中。 Then I will get the details in TableB and group by batch and task identifier and check that that max value in task status is zero (meaning task is still in-progress).然后我将获取 TableB 中的详细信息,并按批次和任务标识符分组,并检查任务状态中的最大值是否为零(意味着任务仍在进行中)。

WITH data as (
    select
        batch_identifier 
    from
        dbo.TableA
    group by
        batch_identifier
    having 
        MAX(CAST(batch_status as int)) = 0)  
select
      details.batch_identifier,
      MIN(CAST(details.task_status as int)) as task_status,
      MIN(details.task_description) as task_description,
      details.task_identifier
from 
        dbo.TableB as details
inner join 
        data on data.batch_identifier = details.batch_identifier  
group by
        details.batch_identifier, 
        task_identifier
having
        MAX(CAST(details.task_status as int)) = 0

RESULT:结果:

batch_identifier    task_status task_description    task_identifier 
----------------    ----------- ----------------    ------------
batch_id2           0           2022-09-15 10:36:00 task_id2
batch_id2           0           2022-09-15 10:37:00 task_id3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM