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

How to implement page screenshot function in JS

"Page screenshot" is a requirement ofte...

Example of implementing QR code scanning effects with CSS3

Online Preview https://jsrun.pro/AafKp/ First loo...

How does Vue3's dynamic components work?

Table of contents 1. Component Registration 1.1 G...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

JS realizes the effect of Baidu News navigation bar

This article shares the specific code of JS to ac...

Detailed explanation of chmod command usage in Linux

chmod Command Syntax This is the correct syntax w...

Brief analysis of mysql scheduled backup tasks

Introduction In a production environment, in orde...

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

Example of how to implement embedded table with vue+elementUI

During my internship in my senior year, I encount...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

Detailed explanation of Linux lsof command usage

lsof (list open files) is a tool to view files op...

SQL left join and right join principle and example analysis

There are two tables, and the records in table A ...

My CSS framework - base.css (reset browser default style)

Copy code The code is as follows: @charset "...

How to implement an array lazy evaluation library in JavaScript

Table of contents Overview How to achieve it Spec...