【问题标题】:Query Help - Where Not Exists?查询帮助 - 哪里不存在?
【发布时间】:2023-12-31 09:08:01
【问题描述】:

我只想退回拥有所有活动帐户的经理。在这种情况下,我希望返回简和她的三个帐户和数据行。

select *
from [Table1] t1 
left join [Table2] t2 
on t1.account = t2.account 
where lower(t1.flag)='y' 
and not exists (select 1 from [Table1] tt1 where tt1.account=t1.account and tti.flag in ('NULL','n'))

我的例外回报必须是: Expected Return Data

【问题讨论】:

  • 源表中的数据是什么?
  • 数据中的NULL是字符串'NULL'还是空值?
  • String NULL 不幸的是@mik
  • 这就是我使用 'NULL' @mik 的原因

标签: mysql sql group-by not-exists appian


【解决方案1】:

您可以使用窗口函数来整理每个经理的计数,然后进行过滤

select t.account, t.flag, t.manager from (
    select *, Count(*) over (partition by manager) cnt,
      Sum(case when flag='y' then 1 end) over(partition by manager) f
    from t
)t
where cnt=f

【讨论】: