sql - Return Parameter If in Child Table Meet Some Condition
问题描述
I have table like this
Parent table
id, column1, etc
- - -
- - -
Detail
id, parent_id, column1, actual_finish (value is true/false)
- - - -
- - - -
I want to check if all column actual_finish
have value true, then return 1 (I think this will be return parameter), else return 0.
For example
parent
id column1 etc,
------------------
1 value1 a
Detail
id, parent_id, column1, actual_finish (value is true/false)
------------------------------------------------------------
1 1 a true
2 1 b false
This will return 0, because second row actual finish value is false, but if second row column actual_finish updated to true, then return 1
I want to create a stored procedure that returns 0 or 1 based on column actual_finish
in the detail
table.
Can someone help me?
解决方案
Query you could use is
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
This is assuming that you return false if there is no row in detail table for parent id
推荐阅读
- javascript - jQuery开/关点击
- java - 如何从 JTextArea 中删除旧文本以使文档大小不超过阈值?(爪哇)
- google-bigquery - Google Big Query:使用自定义维度获取新访客数
- spring - 如何在 Postman 中使用 API 发布数据
- node.js - mongodb中的并发更新
- python - 如何迭代 Twilio SMS 的列表?
- azure-active-directory - 无法从非基本 URL 的 Angular 应用程序登录到 Azure AD
- python - 将python dict“key value”转换为具有“k(value) ey”的dict
- testing - ldif 将“用户”类添加到 org.zapodot.junit.ldap.EmbeddedLdapRule
- performance - 如何加快 Fancybox 上的 Youtube 视频加载时间