【问题标题】:An aggregate may not appear in the WHERE clause unless it is in a subquery聚合可能不会出现在 WHERE 子句中,除非它在子查询中
【发布时间】:2015-08-10 14:56:07
【问题描述】:

我正在尝试使用下面的查询来整理消费者列表基于 1)实际计数然后 2)基于 Sla_state =1 和 result =0 中的值的子计数 ..

查询..

选择consumer作为“Consumer”,class_name作为“Service”,count(consumer)作为“totalcount”,avg(responsetime)作为“AvgResponseTime(ms)”,max(responsetime)作为“Max ResponseTime(ms)”, sla_state 为“sla”,结果为“result_state”,count(1) 为“subcount”
从 DPOWER.business_transaction bt 加入 DPOWER.mmfg_business_transaction mbt on (bt.business_trans_id = mbt.business_trans_id) 在 (bt.class_id = tc.class_id) 和 sla_state = 1 和 result=0 上加入 DPOWER.transaction_class tc 在哪里
(bt.starttime >= '20150701000000000000' and bt.endtime

上面的查询有效..但我只能得到子计数,而不是消费者的总数。下面是三个表结构。谁能弄清楚如何获得总计数。(我尝试了所有可能的方法,例如计数(*)等,但没有成功。如果我使用别名,我会得到“多部分标识符未绑定”错误。

【问题讨论】:

  • consumer in (select count(consumer) from DPOWER.business_transaction where sla_state = 1 and result=0) 似乎不对。
  • @Hanno 我可以使用类似 .................... ................................消费者在(选择计数(1)作为“失败的 Txn 计数”DPOWER.business_transaction 其中sla_state = 1 并且结果=0)
  • 您应该始终为您选择的所有列设置别名。我不知道哪个列来自哪个表,这使得查询很难理解。
  • 感谢您的建议..但是当我尝试包含别名时..我得到“col 的多部分标识符未绑定”...

标签: sql-server count subquery aggregate identifier


【解决方案1】:

你的 WHERE 子句是指这个吗??

where 
consumer in 
    (
        select 
            consumer
        from 
            DPOWER.business_transaction 
        where 
            sla_state = 1 
            and result=0
    ) 
and (bt.starttime >= '20150701000000000000' 
and bt.endtime <= '20150801000000000000') 

【讨论】:

  • 是的..我需要一个“消费者”的计数..当 sla_state = 1 和 result=0 在单独的列中以及消费者的实际计数。
  • 在这种情况下,您必须使用别名加入 DPOWER.business_transaction 两次。请在您的问题中包含此信息,即结果的实际外观。
  • @HannoBinder ..谢谢 ..让我试试
  • 任何帮助将不胜感激..以上是我的表结构
【解决方案2】:

为了让您开始,这里有一个想法:

select  
    bt1.consumer as "Consumer", 
    count(bt1.*) as totalcount,
    count(bt2.consumer) as subcount
from 
    DPOWER.business_transaction bt1
left outer join DPOWER.business_transaction bt2
  on bt1.consumer = bt2.consumer
  and bt2.sla_state = 1 
  and result=0
group by 
    bt1.consumer 
order by consumer

【讨论】:

  • @hanno..我已经编辑了上面的查询...根据您的输入。 “消费者”列仅存在于表“DPOWER.mmfg_business_transaction”中,并且我已加入 DPOWER.business_transaction
  • 其实查询不应该是子查询。将左外连接添加到您的基本查询!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多