SQL Server's case when statement uses implementation statistics

Source: Internet
Author: User

The table game_info is known as follows

Date_info

Result_info
2018-2-4 Win
2018-2-4 Lose
2018-2-4 Win
2018-2-4 Lose
2018-2-5 Lose
2018-2-5 Lose
2018-2-5 Lose

Q. How do I find the following results?

Date_info Win Lose
2018-2-4 2 2
2018-2-5 0 3

First CREATE TABLE game_infdate_info not NULL,

Result_info varchar (5) Check (Result_info in (' Win ', ' lose '));
Inserting data
INSERT into game_info values (' 2018-2-4 ', ' win '), (' 2018-2-5 ', ' lose ');

Query statements

As lose from Game_info Group by Date_info ORDER by date_info ASC;


This is to illustrate the use of the case when statement,

There are two ways to use case

Case  Result_info     if  ' win ' then 1    else  0end

Another usage is

Case if  result_info = ' win ' then 1    else 0end

Case-When statements are also commonly used when judging gender

As in the database of gender in the table is the number 1, 2, but want to query out male, female

Select  (Case gender  if  1 Then ' Male '                                 when  2   then  ' Women 'Else '  Other '              end) As gender   from                   

SQL Server's case when statement uses implementation statistics

Related Article

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.