在嵌套查询中,检查条件的所有值

发布于 2025-01-11 21:38:33 字数 418 浏览 0 评论 0原文

如何摆脱嵌套查询(同意,不同意)?如何重写加入 - 我想不出。也许还有其他最佳解决方案?

select *
from (
    select
        (select count(id) from Agreement a where a.ConclusionCardFile = f.id and a.Status = 1) agreed,
        (select count(id) from Agreement a where a.ConclusionCardFile = f.id and (a.Status <> 1 or a.Status is null)) dis_agreed
    from ConclusionCard_Files f
) t
where t.agreed > 0 and dis_agreed = 0

How can I get rid of nested queries (agree, dis_agreed)? How to rewrite to join - I can not think of it. Maybe there are other optimal solutions?

select *
from (
    select
        (select count(id) from Agreement a where a.ConclusionCardFile = f.id and a.Status = 1) agreed,
        (select count(id) from Agreement a where a.ConclusionCardFile = f.id and (a.Status <> 1 or a.Status is null)) dis_agreed
    from ConclusionCard_Files f
) t
where t.agreed > 0 and dis_agreed = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深府石板幽径 2025-01-18 21:38:34

您可以将条件编写为 where 子句:

select *
from conclusionCard_Files
where exists (
    select *
    from agreement
    where agreement.conclusionCardFile = conclusionCard_Files.id
    having sum(case when status = 1 then 1 else 0 end) > 0
    and    sum(case when status = 1 then 0 else 1 end) = 0
)

You can write the conditions as a where clause:

select *
from conclusionCard_Files
where exists (
    select *
    from agreement
    where agreement.conclusionCardFile = conclusionCard_Files.id
    having sum(case when status = 1 then 1 else 0 end) > 0
    and    sum(case when status = 1 then 0 else 1 end) = 0
)
清眉祭 2025-01-18 21:38:34

也许您只是使用子查询来过滤?将它们移至 WHERE 子句怎么样?

SELECT 
   *
FROM ConclusionCard_Files f
WHERE 
    EXISTS(select * from  Agreement a where (a.ConclusionCardFile = f.id) and a.Status =1)
    AND NOT EXISTS(select * from  Agreement a where (a.ConclusionCardFile = f.id) and (a.Status != 1 or a.Status is null))  

它对性能友好,因为 SqlServer 不会计算所有计数

Maybe you just using sub-queries only to filter? What about to move them to WHERE clause?

SELECT 
   *
FROM ConclusionCard_Files f
WHERE 
    EXISTS(select * from  Agreement a where (a.ConclusionCardFile = f.id) and a.Status =1)
    AND NOT EXISTS(select * from  Agreement a where (a.ConclusionCardFile = f.id) and (a.Status != 1 or a.Status is null))  

It's performance friendly because SqlServer do not count all Counts

裸钻 2025-01-18 21:38:34

如果我理解正确,您可以尝试将 JOINHAVING 条件聚合函数一起使用。

SELECT COUNT(CASE WHEN a.Status = 1 THEN ID END) agreed,
       COUNT(CASE WHEN a.Status <> 1 or a.Status is null THEN ID END) dis_agreed
FROM Agreement a 
INNER JOIN ConclusionCard_Files f
ON a.ConclusionCardFile = f.id
HAVING 
    COUNT(CASE WHEN a.Status = 1 THEN ID END) > 0
AND 
   COUNT(CASE WHEN a.Status <> 1 or a.Status is null THEN ID END) = 0

编辑

如果您想根据您的情况从 ConclusionCard_Files 获取数据, 。您可以尝试让子查询中的条件聚合函数来自表Agreement的每个ConclusionCardFile,然后执行JOIN

SELECT f.*
FROM (
    SELECT COUNT(CASE WHEN a.Status = 1 THEN ID END) agreed,
           COUNT(CASE WHEN a.Status <> 1 or a.Status is null THEN ID END) dis_agreed,
           a.ConclusionCardFile
    FROM Agreement a 
    GROUP BY a.ConclusionCardFile
) a 
INNER JOIN ConclusionCard_Files f
ON a.ConclusionCardFile = f.id
WHERE a.agreed > 0 AND a.dis_agreed = 0

If I understand correctly, you can try to use JOIN with HAVING condition aggregate function.

SELECT COUNT(CASE WHEN a.Status = 1 THEN ID END) agreed,
       COUNT(CASE WHEN a.Status <> 1 or a.Status is null THEN ID END) dis_agreed
FROM Agreement a 
INNER JOIN ConclusionCard_Files f
ON a.ConclusionCardFile = f.id
HAVING 
    COUNT(CASE WHEN a.Status = 1 THEN ID END) > 0
AND 
   COUNT(CASE WHEN a.Status <> 1 or a.Status is null THEN ID END) = 0

EDIT

if you want to get data from ConclusionCard_Files based on your condition. you can try to let condition aggregate function in subquery each ConclusionCardFile from table Agreement then do JOIN

SELECT f.*
FROM (
    SELECT COUNT(CASE WHEN a.Status = 1 THEN ID END) agreed,
           COUNT(CASE WHEN a.Status <> 1 or a.Status is null THEN ID END) dis_agreed,
           a.ConclusionCardFile
    FROM Agreement a 
    GROUP BY a.ConclusionCardFile
) a 
INNER JOIN ConclusionCard_Files f
ON a.ConclusionCardFile = f.id
WHERE a.agreed > 0 AND a.dis_agreed = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文