SQL查询根据条件显示日期和计数

时间:2014-04-15 10:06:39

标签: sql-server-2008

我想显示,因为我的下面就像使用两个查询我想显示我的查询

select * from 
(
   select CONVERT(VARCHAR(10), dialdate, 105)  as toc, 
          count(endtime) as 'TotalNoOfCalls' 
   from processeddata_table 
   where DialDate between '2012-03-03' 
         and '2014-04-14'  
         and endtime!= '' 
         and toc != ''  
         and adjustedduration !='00:00:00' 
         and toc like 'voip%' 
         and endtime='Comverse'
   group by dialdate 
)  processeddata_table 
union 
   select CONVERT(VARCHAR(10), dialdate, 105) as toc, 
          count(endtime) as 'TotalNoOfCalls' 
   from processeddata_table
   where DialDate between '2012-03-03' 
         and '2014-04-14'  
         and endtime!= '' 
         and toc != ''  
         and adjustedduration !='00:00:00' 
         and (endtime='Bharthi ' or endtime='TATA') 
         and (toc = 'STD' 
           or toc = 'ISD' 
           or toc = 'LOCAL' 
           or toc = 'Unmatured'  
           or toc = 'TOLL' 
           or toc = 'ACB' 
           or toc = 'ACB-IN' 
           or toc = 'BTB' 
           or toc = 'INCOMING') 
     group by dialdate 

输出:

Date         Total

04-04-2014     2

04-04-2014     1

我需要结果:

Date         Total

04-04-2014     3

2 个答案:

答案 0 :(得分:0)

查看WHERE子句,您应该只能通过一个查询获得此内容。

我可能有一些不匹配的括号,但我没有列出所有的结束时间,但你明白了。

select CONVERT(VARCHAR(10), dialdate, 105) as toc, 
      count(endtime) as 'TotalNoOfCalls' 
from processeddata_table
where DialDate between '2012-03-03' 
     and '2014-04-14'  
     and endtime!= '' 
     and toc != ''  
     and adjustedduration !='00:00:00' 
     and (
     (endtime='Comverse')
     OR         
     (endtime IN ('Bharthi ','TATA') 
     and (toc IN ('STD' ,'ISD' , 'Comverse',...))
     ) )
 group by dialdate 

答案 1 :(得分:0)

您可以将where子句组合为ElectricLlama提到的。

另一种方法是选择原始值并将该查询嵌入子查询中并对该子查询使用聚合。

select
    CONVERT(VARCHAR(10), dialdate, 105)  as toc, 
    count(endtime) AS TotalNoOfCalls
from 
(
        select
            dialdate, 
            endtime
        from
            processeddata_table 
        where
            DialDate between '2012-03-03' 
            and '2014-04-14'  
            and endtime!= '' 
            and toc != ''  
            and adjustedduration !='00:00:00' 
            and toc like 'voip%' 
            and endtime='Comverse'

    union 

        select
            dialdate, 
            endtime
        from
            processeddata_table
        where
            DialDate between '2012-03-03' 
            and '2014-04-14'  
            and endtime!= '' 
            and toc != ''  
            and adjustedduration !='00:00:00' 
            and (endtime='Bharthi ' or endtime='TATA') 
            and (
                -- Use IN instead of endless ORs
                toc = 'STD' 
                or toc = 'ISD' 
                or toc = 'LOCAL' 
                or toc = 'Unmatured'  
                or toc = 'TOLL' 
                or toc = 'ACB' 
                or toc = 'ACB-IN' 
                or toc = 'BTB' 
                or toc = 'INCOMING'
            )
    ) DATA
group by
    dialdate