MySQL column to row conversion and year-month grouping example

MySQL column to row conversion and year-month grouping example

As shown below:

SELECT count(DISTINCT(a.rect_id)) zcount, a.job_dept,

 DATE_FORMAT(submit_date, '%Y-%m') zsubmit_date

 FROM

 Table Namea

 WHERE

 a.status = 3

 AND a.rstatu = 2

 AND a.job_dept IN ('19', '20', '21')

 GROUP BY

 a.job_dept,

 DATE_FORMAT(submit_date, '%Y-%m')

The key is that DATE_FORMAT (submit_date, '%Y-%m') groups and sorts the time, year and month.

SELECT      
zsubmit_date,      
MAX(CASE WHEN job_dept = '19' THEN zcount ELSE 0 END ) 19zcount,      
MAX(CASE WHEN job_dept = '20' THEN zcount ELSE 0 END ) 20zcount,      
MAX(CASE WHEN job_dept = '21' THEN zcount ELSE 0 END ) 21zcount  

FROM    
(     
SELECT       
count(DISTINCT(a.rect_id)) zcount, a.job_dept,        
DATE_FORMAT(submit_date, '%Y-%m') zsubmit_date     
FROM       
Table Namea     
WHERE       
a.status = 3       
AND a.rstatu = 2       
AND a.job_dept IN ('19', '20', '21')     
GROUP BY     
a.job_dept,     
DATE_FORMAT(submit_date, '%Y-%m')    
) q GROUP BY    
zsubmit_date 

The above MySQL column to row conversion and year-month grouping example is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to convert a column of comma-separated values ​​into columns in MySQL
  • Detailed explanation of MySQL row locks when encountering composite primary keys and multi-column indexes
  • Mysql method to calculate the difference between two adjacent rows of a column
  • How to sort a row or column in mysql
  • MySQL column to row conversion, method of merging fields (must read)
  • MySQL column to row conversion tips (share)
  • Detailed examples of converting rows to columns and columns to rows in MySQL
  • Implementation of dynamic conversion of mysql rows and columns (contingency table, cross table)
  • Database implementation of row and column conversion (mysql example)
  • How to convert rows to columns in MySQL

<<:  Detailed explanation of nginx reverse proxy webSocket configuration

>>:  Implementation of k8s node rejoining the master cluster

Recommend

Should the Like function use MySQL or Redis?

Table of contents 1. Common mistakes made by begi...

Detailed explanation of nginx request header data reading process

In the previous article, we explained how nginx r...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

Detailed explanation of root directory settings in nginx.conf

There are always some problems when configuring n...

Various ways to modify the background image color using CSS3

CSS3 can change the color of pictures. From now o...

Detailed explanation of Bind mounts for Docker data storage

Before reading this article, I hope you have a pr...

Define your own ajax function using JavaScript

Since the network requests initiated by native js...

JavaScript to achieve a simple magnifying glass effect

There is a picture in a big box. When you put the...

How to use Font Awesome 5 in Vue development projects

Table of contents Install Dependencies Configurat...

Teach you how to write maintainable JS code

Table of contents What is maintainable code? Code...

MySql common query command operation list

MYSQL commonly used query commands: mysql> sel...

MySQL performance optimization tips

MySQL Performance Optimization MySQL is widely us...

How to start a transaction in MySQL

Preface This article mainly introduces how to sta...