当前位置:网站首页>Usage of case when then else end statement
Usage of case when then else end statement
2022-07-04 10:57:00 【ths512】
- SELECT a.managecom,
- a.subtype,
- count(*) loadsucc,
- sum(case when a.state in ('4', '5', '6', '7', '8', '9') then 1 else 0 end) recogsucc,
- sum(case when a.state in ('3', '12', '13') then 1 else 0 end) recogfail,
- sum(case when a.state in ('1', '2') then 1 else 0 end) waitrecog
- FROM ocr_docdetail a, ocr_loaddetail c
- WHERE 1 = 1
- and a.managecom like '86%'
- and a.managecom = c.managecom
- and a.bussno = c.bussno
- and a.subtype = c.subtype
- and c.loadstate = 0
- and c.scandate >= date '2012-07-29'
- and c.scandate <= date '2013-01-07'
- group by a.managecom, a.subtype
- order by a.managecom, a.subtype;
case There are two formats . Simple case Functions and case The search function .
-- Simple case function
case sex
when '1' then ' male '
when '2' then ' Woman '
else ' other ' end
--case The search function
case when sex = '1' then ' male '
when sex = '2' then ' Woman '
else ' other ' end
These two ways , Can achieve the same function . Simple case The writing of function is relatively simple , But and case Search function , There will be some functional limitations , For example, write a decision form .
There is another issue that needs attention ,case Function returns only the first value that matches the condition , The rest case Parts will be automatically ignored .
-- for instance , The following paragraph sql, You'll never get “ The second category ” This result
case when col_1 in ( 'a', 'b') then' The first category '
when col_1 in ('a') then ' The second category '
else ' other ' end
So let's see , Use case Functions can do something .
One , The known data is grouped in another way , analysis .
Here's the data :( To see better , I didn't use the country code , It's the name of the country primary key)
Country (country) | Population (population) |
China | 600 |
The United States | 100 |
Canada | 100 |
The British | 200 |
The French | 300 |
Japan | 250 |
Germany | 200 |
Mexico | 50 |
India | 250 |
According to the population data of this country , Count the population of Asia and North America . We should get the following result .
continent | Population |
Asia | 1100 |
In North America | 250 |
other | 700 |
I want to solve this problem , What would you do ? Generate a continent with code Of view, It's a solution , But it's hard to change the way of statistics dynamically .
If used case function ,sql The code is as follows :
select sum(population),
case country
when ' China ' then' Asia '
when ' India ' then' Asia '
when ' Japan ' then' Asia '
when ' The United States ' then' In North America '
when ' Canada ' then' In North America '
when ' Mexico ' then' In North America '
else ' other ' end
from table_a
group by case country
when ' China ' then' Asia '
when ' India ' then' Asia '
when ' Japan ' then' Asia '
when ' The United States ' then' In North America '
when ' Canada ' then' In North America '
when ' Mexico ' then' In North America '
else ' other ' end;notes :
In the example above , Actually select There are two fields :sum(population), case country;
country There are many values in the field , Yes “ China , The United States , Japan , Canada ...” wait ,select After case when then else end In fact, it is equivalent to country There are three types of values for :" Asia , In North America , other ";
There is no from hinder case when sentence , The result set is like this ,sum What you get is the population population Total of , So we need to population grouping , There's the back group by:

hinder case when sentence , In fact, the fields queried earlier case country Grouping , Divide into three groups :" Asia , In North America , other ", Then you can get the result
alike , We can also use this method to determine the salary grade , And count the number of people at each level .sql The code is as follows ;
select
case when salary <= 500 then '1'
when salary > 500 and salary <= 600 then '2'
when salary > 600 and salary <= 800 then '3'
when salary > 800 and salary <= 1000 then '4'
else null end salary_class,
count(*)
from table_a
group by
case when salary <= 500 then '1'
when salary > 500 and salary <= 600 then '2'
when salary > 600 and salary <= 800 then '3'
when salary > 800 and salary <= 1000 then '4'
else null end;
Two , Use one sql Statement to complete the grouping of different conditions .
Here's the data
Country (country) | Gender (sex) | Population (population) |
China | 1 | 340 |
China | 2 | 260 |
The United States | 1 | 45 |
The United States | 2 | 55 |
Canada | 1 | 51 |
Canada | 2 | 49 |
The British | 1 | 40 |
The British | 2 | 60 |
Grouped by country and gender , The results are as follows
Country | male | Woman |
China | 340 | 260 |
The United States | 45 | 55 |
Canada | 51 | 49 |
The British | 40 | 60 |
In general , use union You can also query with a single statement . But that increases consumption ( Two select part ), and sql The statements will be longer .
Here's one that uses case Function to complete this function
select country,
sum( case when sex = '1' then
population else 0 end), -- Male population
sum( case when sex = '2' then
population else 0 end) -- Female population
from table_a
group by country;
So we use select, Complete the output form of the two-dimensional table , It fully shows that case The power of functions .
3、 ... and , stay check Use in case function .
stay check Use in case Functions are very good solutions in many cases . Maybe a lot of people don't have to check, Well, I suggest you try the following example as well sql Use in check.
Let's take an example
company a, This company has a policy , The salary of female staff must be higher than 1000 block . If used check and case To show , As shown below
constraint check_salary check
( case when sex = '2'
then case when salary > 1000
then 1 else 0 end
else 1 end = 1 )
If only used check, As shown below
constraint check_salary check
( sex = '2' and salary > 1000 )
The conditions of female staff are met , Male staff can't input .
边栏推荐
- Performance test method
- Const's constant member function after the function; Form, characteristics and use of inline function
- [Galaxy Kirin V10] [server] system partition expansion
- Hidden C2 tunnel -- use of icmpsh of ICMP
- /*The rewriter outputs the contents of the IA array. It is required that the type defined by typedef cannot be used in the outer loop*/
- Canoe-the second simulation project-xvehicle-1 bus database design (idea)
- [Galaxy Kirin V10] [desktop] can't be started or the screen is black
- Performance test process
- 2022 ape circle recruitment project (software development)
- Installation of ES plug-in in Google browser
猜你喜欢

Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN

Postman interface test

Installation of ES plug-in in Google browser

Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
![[Galaxy Kirin V10] [desktop] printer](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Galaxy Kirin V10] [desktop] printer

JMeter assembly point technology and logic controller

How do microservices aggregate API documents? This wave of show~

Summary of several job scheduling problems

Huge number multiplication (C language)
![[Galaxy Kirin V10] [server] NFS setup](/img/ed/bd7f1a1e4924a615cb143a680a2ac7.jpg)
[Galaxy Kirin V10] [server] NFS setup
随机推荐
Read a piece of text into the vector object, and each word is stored as an element in the vector. Convert each word in the vector object to uppercase letters. Output the converted elements in the vect
Jianzhi offer 04 (implemented in C language)
Canoe: distinguish VT, VN and vteststudio from their development history
Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
On binary tree (C language)
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
试题库管理系统–数据库设计[通俗易懂]
[test theory] test process management
Linked list operation can never change without its roots
LVS+Keepalived实现四层负载及高可用
software test
Solaris 10网络服务
Add t more space to your computer (no need to add hard disk)
How do microservices aggregate API documents? This wave of show~
Recursive method to achieve full permutation (C language)
Canoe - the third simulation project - bus simulation - 3-2 project implementation
Canoe - the third simulation project - bus simulation - 3-1 project implementation
3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
Performance test method
Seven examples to understand the storage rules of shaped data on each bit