How to add conditional expressions to aggregate functions in MySql

How to add conditional expressions to aggregate functions in MySql

MySQL filtering timing of where conditions and having conditions when used with aggregate functions

where filters before aggregation

When a query contains aggregate functions and where conditions, such as
select max(cid) from t where t.id<999
At this time, filtering will be performed first and then aggregation. First filter out the records with ID < 999, then find the largest cid and return it.

Having filters after aggregation

Having is used when grouping to filter the grouping results, and usually contains aggregate functions.

SELECT ip,MAX(id) FROM app
GROUP BY ip
HAVING MAX(id)>=5

First group, then aggregate, and then filter the result set where the aggregate result is greater than or equal to 5

The difference between the two:

where is executed first, and then the aggregate function is executed. Having is executed after the aggregate function is executed.

The following is a supplement

There is a requirement that a table has a status field (1: success, 2: failure, and so on). Now we need to use date grouping to count the number of items in different states.

First write a subquery

select aa.logDate,aa.totalLogs 
 ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=1) pendingLogs
 ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=2) successLogs
 ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=3) errorLogs
 ,(select count(1) from dxp.dxp_handlermodel where aa.logDate=DATE_FORMAT( startTime, '%Y-%m-%d') and executeStatus=4) callbackErrorLogs
from
(
 select
 DATE_FORMAT( a.startTime, '%Y-%m-%d') logDate,
 count(1) totalLogs
 from dxp.dxp_handlermodel a 
 group by DATE_FORMAT( a.startTime, '%Y-%m-%d') 
)

The execution is quite slow, so I wondered if I could add conditions to count, and found the following:

select
DATE_FORMAT( startTime, '%Y-%m-%d') logDate,
 count(1) totalLogs,
 count(if(executeStatus=1,true,null)) pendingLogs,
 count(if(executeStatus=2,true,null)) successLogs,
 count(if(executeStatus=3,true,null)) errorLogs,
 count(if(executeStatus=4,true,null)) callbackErrorLogs
from dxp.dxp_handlermodel
group by DATE_FORMAT( startTime, '%Y-%m-%d')

Easy to understand and highly efficient in execution

Other aggregate functions can also be used, such as SUM and other aggregate functions

Practical example:

select count(if(create_date < '2017-01-01' and host_profile_id = '9294d2bf-f457-4fe5-9a36-e5f832310dc2',true,null)) from profile_visit_log 
-- Equivalent to select count(if(create_date < '2017-01-01',true,null)) count from profile_visit_log where host_profile_id = '9294d2bf-f457-4fe5-9a36-e5f832310dc2'

Well, this is the end of this article. I hope you will support 123WORDPRESS.COM in the future.

You may also be interested in:
  • MySQL aggregate function sorting
  • MySQL grouping queries and aggregate functions
  • Analysis of MySQL query sorting and query aggregation function usage
  • Usage and performance optimization techniques of aggregate function count in MySQL
  • MySQL essential basics: grouping function, aggregate function, grouping query detailed explanation

<<:  How to handle the loss of parameters when refreshing the page when passing parameters to vue router

>>:  Vue Element front-end application development preparation for the development environment

Recommend

Solution for using Baidu share on Https page

Since enabling https access for the entire site, ...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

In-depth understanding of JavaScript event execution mechanism

Table of contents Preface The principle of browse...

An article to deal with Mysql date and time functions

Table of contents Preface 1. Get the current time...

How to receive binary file stream in Vue to realize PDF preview

Background Controller @RequestMapping("/getP...

How to install binary MySQL on Linux and crack MySQL password

1. Make sure the system has the required libaio s...

Detailed explanation of setting Context Path in Web application

URL: http://hostname.com/contextPath/servletPath/...

MySQL select results to perform update example tutorial

1. Single table query -> update UPDATE table_n...

Detailed explanation of MySQL covering index

concept If the index contains all the data that m...

Let's talk briefly about the changes in setup in vue3.0 sfc

Table of contents Preface Standard sfc writing me...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...

Detailed explanation of the new array methods in JavaScript es6

Table of contents 1. forEach() 2. arr.filter() 3....

Learn MySQL database in one hour (Zhang Guo)

Table of contents 1. Database Overview 1.1 Develo...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...