Return 参数 If in Child Table 满足某些条件

Return Parameter If in Child Table Meet Some Condition

我有table这样的

Parent table

id, column1,  etc
-    -         -     
-    -         -     

详情

id, parent_id, column1, actual_finish (value is true/false)
 -     -            -           -      
 -     -            -           -          

我想检查是否所有列 actual_finish 的值为 true,然后 return 1(我认为这将是 return 参数),否则 return 0。

例如

parent

id   column1  etc,
------------------
1    value1    a 

详情

id, parent_id, column1, actual_finish (value is true/false)
------------------------------------------------------------
1       1         a           true
2       1         b           false

这将return 0,因为第二行实际完成值是false,但是如果第二行列actual_finish更新为true,那么return 1

我想根据 detail table.

中的列 actual_finish 创建一个 return 为 0 或 1 的存储过程

有人可以帮助我吗?

您可以使用的查询是

Select returnvalue= case when totalCount=trueCount then 1 else 0 end 
from
(select 
    trueCount=count (case when actual_finish ='true' then 1 else 0 end), 
    totalCount = count(1)
from
parent p left join detail d
    on p.id=d.parent_id
group by p.id
)T

这是假设您 return 如果没有详细的行 table 父 ID

则为 false

尝试使用,

select min(case when actual_finish='true' then 1 else 0 end) from tab_Detail where parent_id=1;

SP,类似..

create procedure getStatusById
(@id int)
as
begin
declare @res as int=0
set @res=(select min(case when actual_finish='true' then 1 else 0 end) from tab_Detail
where parent_id=@id);
return @res
print(@res)
end

Fiddle

我认为要求是,即使有一行存在 actual_fnish 为假然后 return 0.

declare @res as int=1
If exists(select 1 from dbo.tbldetail where actual_finish='false')
set @res=0

actual_finish 应该是 BIT 数据类型(0 或 1)

我建议使用 exists。处理 parents 中的多行:

select p.*,
       (case when exists (select 1
                          from details d
                          where d.parent_id = p.id and
                                d.actual_finish = 'false'
                         )
             then 0 else 1
        end) as flag
from parents p;