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

React realizes the whole process of page watermark effect

Table of contents Preface 1. Usage examples 2. Im...

Image scrolling effect made with CSS3

Achieve resultsImplementation Code html <base ...

...

Detailed explanation of various loop speed tests in JS that you don’t know

Table of contents Preface 1. for loop 2. while lo...

Detailed steps for Spring Boot packaging and uploading to Docker repository

Important note: Before studying this article, you...

How to write transparent CSS for images using filters

How to write transparent CSS for images using filt...

Design Theory: Text Legibility and Readability

<br />Not long ago, due to business needs, I...

Docker data volume container creation and usage analysis

A data volume container is a container specifical...

Create a custom system tray indicator for your tasks on Linux

System tray icons are still a magical feature tod...

Tutorial on installing MySQL 5.6 using RPM in CentOS

All previous projects were deployed in the Window...

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx The first method di...

A simple way to build a Docker environment

First, let’s understand what Docker is? Docker is...

Common rule priority issues of Nginx location

Table of contents 1. Location / Matching 2. Locat...