SQL Select Case When statement

Source: Internet
Author: User

Youdao Written Questions:

Server Monitoring table Server_status, when the server state occurs server_status changes in the data table will be inserted a record, state 0 indicates that the shutdown 1 is normal, SQL query Server A's downtime start time and end time, There is a status record for more than one server in the table.

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 = s2.svr_id  and = 1  and = 0  and S1.svr_name='A'

, today I saw someone using the case grammar, so I tried to write a bit

SelectSvr_name, CaseStatus when 1  ThenStatus_timeEnd  asStart_time, CaseStatus when 0  ThenStatus_timeEnd  asEnd_time fromServer_statuswhereSvr_name= 'A'

Don't know if there is any better way ...

SQL Select Case When statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.