【问题标题】:How to find group which is having Status Active? [closed]如何找到状态为活动的组? [关闭]
【发布时间】:2018-04-22 07:05:47
【问题描述】:

我有一个 ID,它在状态为 Active 的表中有多个 Accountid。我需要找到特定 ID 的活跃组。

请找到我下面的查询。它是否正确或需要修改以获得更好的优化结果。

select id,count(Accountid) from CustomerAccount
where status='Active'
group by id
having count(*)>( select min(maxxount) from(
            select id,count(accountid) as maxxount
                        from CustomerAccount
                        group by id)A)

【问题讨论】:

  • 给出一些样本数据和基于此的预期输出。如果可能的话,在 sqlfiddle.com 创建一个小提琴演示。还要提及您正在使用的 RDMS/版本
  • 您是否尝试获取具有超过 1 个活动状态条目的帐户列表?
  • @Utsav 例如:帐户 id 1 和 5 有两个状态为 active 的 id。我能得到 1 和 5v 的结果吗?上面给出了结果,但我可以通过非常简单的方法找到具有多个活动状态的 id。
  • @DEEPAKLAKHOTIA 是的:你是对的

标签: sql join group-by aggregate-functions having-clause


【解决方案1】:

使用这个查询:

select id,count(Accountid) from CustomerAccount
where status='Active'
group by id
having count(*) >1

【讨论】:

  • :好的,谢谢。给我结果
  • 请给我投票
猜你喜欢
  • 1970-01-01
  • 2016-01-10
  • 2011-09-19
  • 2018-05-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多