MySQL solution for creating horizontal histogram

MySQL solution for creating horizontal histogram

Preface

Histogram is a basic statistical information provided by RDBMS. Its most typical use is to estimate the selectivity of query predicates in order to select an optimized query execution plan. Common types of histograms include: equal-width histogram, equal-height histogram, V-optimized histogram, MaxDiff histogram, etc.

Note: The test database version is MySQL 8.0

If you need to create tables and enter data under user scott, you can refer to:

Scott creates tables and enters data SQL script

1. Demand

Use SQL to generate a horizontally extending histogram.

For example, a horizontal histogram is used to display the number of volunteers in each department, with an asterisk "*" representing an employee.

The returned result set should be like:

±-------±-------+
| deptno | cnt |
±-------±-------+
| 10 | *** |
| 20 | ***** |
| 30 | ****** |
±-------±-------+

2. Solution

Use group by to group, find the total number, and then fill it with lpad

select deptno,
 lpad('*',count(*),'*') as cnt
 from emp
 group by deptno;

Testing Log:

mysql> select deptno,
 -> lpad('*',count(*),'*') as cnt
 -> from emp
 -> group by deptno;
+--------+--------+
| deptno | cnt |
+--------+--------+
| 10 | *** |
| 20 | ***** |
| 30 | ****** |
+--------+--------+
3 rows in set (0.00 sec)

Summarize

This is the end of this article about creating a horizontal histogram in MySQL. For more relevant content about creating a horizontal histogram in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Convert XHTML CSS pages to printer pages

>>:  The whole process of configuring reverse proxy locally through nginx

Recommend

Using CSS3 and JavaScript to develop web color picker example code

The web color picker function in this example use...

The linkage method between menu and tab of vue+iview

Vue+iview menu and tab linkage I am currently dev...

Detailed explanation of the use of shared memory in nginx

In the nginx process model, tasks such as traffic...

Advanced crawler - Use of Scrapy_splash component for JS automatic rendering

Table of contents 1. What is scrapy_splash? 2. Th...

How to use ss command instead of netstat in Linux operation and maintenance

Preface When operating and managing Linux servers...

Sitemesh tutorial - page decoration technology principles and applications

1. Basic Concepts 1. Sitemesh is a page decoratio...

Implementation of communication between Vue and Flask

Install axios and implement communication Here we...

Vue/react single page application back without refresh solution

Table of contents introduction Why bother? Commun...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

Summary of the differences between Mysql primary key and unique key

What is a primary key? A primary key is a column ...

In-depth understanding of MySQL master-slave replication thread state transition

Preface The basic principle of MySQL master-slave...

Install zip and unzip command functions under Linux and CentOS (server)

Install zip decompression function under Linux Th...