2011-09-02 79 views
0

我有一个名为Call_Data表和数据的样子:组合两个时间间隔查询

Arr_Date     Interval Han_Time SL_Time 
2011-03-16 18:39:31.000 1830  1200  245 
2011-03-16 20:06:03.000 2000  261  85 
2011-03-31 00:35:42.000 0030  1900  400 
2011-03-31 02:13:06.000 0200  2000  350 

现在我想知道,有Han_time> 1800 SL_Time> 300

这样的记录数我写了两个查询来做到这一点:

Select Arr_Date, Interval, 
    Count(*) AS Han_Time_1800 
    From Call_Data 
    where Han_Time>1800 
    Group by Arr_Date,Interval 
    Order by Arr_Date,Interval 

Select Arr_Date, Interval, 
Count(*) AS SL_Time_300 
From Call_Data 
where SL_Time>300 
Group by Arr_Date,Interval 
Order by Arr_Date,Interval 

是否有一种方法,我可以在一个查询中得到两个值?

回答

2
select Arr_Date, 
     Interval, 
     sum(case when Han_Time > 1800 then 1 else 0 end) as Han_Time_1800, 
     sum(case when SL_Time > 300 then 1 else 0 end) as SL_Time_300 
from Call_Data 
where Han_Time > 1800 or 
     SL_Time > 300 
group by Arr_Date, Interval 
order by Arr_Date, Interval 
0

这会不会忍不住道:

Select Arr_Date, Interval, 
    Count(*) AS Han_Time_1800 
    From Call_Data 
    where Han_Time>1800 
    and SL_Time>300 
    Group by Arr_Date,Interval 
    Order by Arr_Date,Interval 
+0

没有如果Han_time is1900什么,SL_time是250 – Peter