除非在子查询中,否则聚合可能不会出现在WHERE子句中

时间:2015-08-10 14:56:07

标签: sql-server count subquery aggregate identifier

我正在尝试使用以下查询来根据消费者列表进行排序 1)实际计数然后 2)基于Sla_state = 1和result = 0的值的子计数。

查询..

选择消费者为"消费者",class_name为"服务",计数(消费者)为" totalcount",avg(响应时间)为" AvgResponseTime (ms)",max(响应时间)为"最大响应时间(ms)" ,sla_state为" sla",结果为" result_state" ,count(1)as" subcount"
从  DPOWER.business_transaction bt加入DPOWER.mmfg_business_transaction mbt (bt.business_trans_id = mbt.business_trans_id)加入DPOWER.transaction_class tc on(bt.class_id = tc.class_id)和sla_state = 1且result = 0 其中
 (bt.starttime> =' 20150701000000000000'和bt.endtime< =' 20150801000000000000')按消费者分组,sla_state,result,class_name按消费者排序

以上查询有效..但我只能获得子金额而不是消费者的总数。下面是三个表格结构。任何人都可以弄清楚如何获得总计数。(我尝试了所有可能的方式,如count(*)等,但没有工作..如果我使用别名我得到"多部分标识符没有绑定"错误。

2 个答案:

答案 0 :(得分:0)

你的意思是WHERE子句吗?

 <Workspace version = "1.0">
    <FileRef
-      location = "self:/Users/xyz/Development/Projects/Project.xcodeproj">
+      location = "self:sdk.xcodeproj">
    </FileRef>
 </Workspace>

答案 1 :(得分:0)

为了让你开始,这是一个想法:

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