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

Vue Element-ui table realizes tree structure table

This article shares the specific code of Element-...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

Usage and best practice guide for watch in Vue3

Table of contents Preface🌟 1. API Introduction 2....

8 examples of using killall command to terminate processes in Linux

The Linux command line provides many commands to ...

Example of nginx ip blacklist dynamic ban

When a website is maliciously requested, blacklis...

Native JS to achieve digital table special effects

This article shares a digital clock effect implem...

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

Why the table file size remains unchanged after deleting data in MySQL

For databases that have been running for a long t...

VMware Workstation is not compatible with Device/Credential Guard

When installing a virtual machine, a prompt appea...

Detailed explanation of MySQL high availability architecture

Table of contents introduction MySQL High Availab...

How Database SQL SELECT Queries Work

As Web developers, although we are not profession...

How to install and configure the supervisor daemon under centos7

Newbie, record it yourself 1. Install supervisor....

Specific method of viewing user authorization information in mysql

Specific method: 1. Open Command Prompt 2. Enter ...