001 _ {Case ~~ of SQL Series ~~ When ~~ Then}

Source: Internet
Author: User

1,Equivalent judgment, equivalent to switch case

Case expression
When value1 then returnvalue1
When value2 then returnvalue2
When value3 then returnvalue3
Else defaultreturnvalue
End
For example:
Case level
When 1 then 'Uncle'
When 2 then 'Uncle two'
When 3 then 'Uncle'
End

It is equivalent to a switch and can only be equivalent.

 

  SelectUsername, Uncle level = (
  1. Case Level
  2. When1Then 'Uncle'
  3. When2Then 'Uncle two'
  4. When3Then 'Uncle'
  5. End
  6. )
  7. FromUserinfo
 
Select username, Uncle level = (Case levelwhen 1 then 'Uncle 'when 2 then' second uncle 'when 3 then 'Uncle 'end) from userinfo

The result is :~~~

 

2. interval judgment

Case
When condition1 then returnvalue1
When condition 2 then returnvalue2
When Condition 3 then returnvalue3
Else defaultreturnvalue
End
Equivalent to if... Else if... Else .... (Interval judgment is supported)
 
Note: The data types returned after then must be consistent. The data types of returnvalue1, returnvalue2, and returnvalue3 must be consistent.

  1. SelectUsername, age = (
  2. Case
  3. WhenAgeBetween28And30Then 'Uncle'
  4. WhenAgeBetween25And27Then 'Big youth'
  5. WhenAgeBetween20And24Then 'Sao year'
  6. WhenAge <20Then 'Kid'
  7. Else 'Baby'
  8. End
  9. )
  10. FromUserinfo2
 
Select username, age = (case when age between 28 and 30 then 'Uncle 'When age between 25 and 27 then 'Young up' when age between 20 and 24 then' Sao Nian 'when age <20 then 'kid' else 'babe' end) from userinfo2

 

3. Practical cases

1. user level in general forums

Forum users
Case bbslevel
When 1 then 'creden'
When 2 then 'prawns'
When 3 then 'cainiao'
End

2. Count the total sales amount of each salesperson in the order table, and list the salesperson's name, total sales amount, and title (> gold medals,> silver medals,> bronze medals, otherwise normal)

3. E-commerce users have different levels based on their total consumption.

4. players in the gaming industry

 

Ticket No. Amount
RMB1 10
RMB2 20
RMB3-30
RMB4-10
Output the preceding table in the following format:
Bill No. Income and expenditure
RMB1 10 0
RMB2 20 0
RMB3 0 30
RMB4 0 10

 

  1. SelectNumber,
  2. (
  3. Case
  4. WhenAmount> 0ThenAmount
  5. Else0
  6. End
  7. ) Income,
  8. (
  9. Case
  10. WhenAmount <0Then ABS(Amount)
  11. Else0
  12. End
  13. ) Expenditure
  14. FromTest
 
Select number (case when amount> 0 then amountelse 0end), (case when amount <0 then ABS (amount) else 0end) from test

 

 

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.