有道筆試題:
服務(wù)器監(jiān)控表server_status中,當服務(wù)器狀態(tài)發(fā)生server_status變化時數(shù)據(jù)表中將被插入一條記錄,狀態(tài)0表示停機 1表示正常,用SQL查詢Server A 的停機開始時間和結(jié)束時間,表中存在多臺Server的狀態(tài)記錄.
SVR_ID SVR_NAME STATUS_TIME STATUS
1 A 2013-10-31 00:00:00 1
2 B 2013-10-31 00:00:00 1
1 A 2014-11-31 00:00:00 0
2 B 2014-11-31 00:00:00 0
3 C 2014-11-31 00:00:00 0
... ... ... ...
我做的話只會
select s1.svr_name, s1.status_time start_date, s2.status_time end_date
from server_status s1, server_status s2
where s1.svr_id = s2.svr_id
and s1.status = 1
and s2.status = 0
and s1.svr_name='A'
,今天看到別人用了case when語法,于是我也試著寫了一下
select svr_name,
case status
when 1 then
status_time
end as start_time,
case status
when 0 then
status_time
end as end_time
from server_status
where svr_name = 'A'
不知道有沒有更好的做法...