2011-09-02 106 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