Use in SQL: Case

Source: Internet
Author: User

Store_informationTable

Store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999

If we want to multiply the sales value of 'Los Angeles' by 2, and the sales value of 'san Diego by 1.5, We will merge the following SQL statement:

Select store_name, Case store_name
When 'Los Angeles' then sales * 2
When 'san Diego 'Then sales * 1.5
Else sales
End
New sales,
Date
From store_information

Or you can write: Select store_name, sales = case store_name
When 'Los Angeles' then sales * 2
When 'san Diego 'Then sales * 1.5
Else sales
End, date from store_information

"New Sales" is usedCaseThe name of the second digit.

Result:

Store_name New Sales Date
Los Angeles $3000 Jan-05-1999
San Diego $375 Jan-07-1999
San Francisco $300 Jan-08-1999
Boston $700 Jan-08-1999

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.