WIDTH BUCKET Function Example | Teradata Vantage - 17.10 - Example: Using WIDTH BUCKET to Create a Histogram for Employee Salaries within a Range - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - SQL Functions, Expressions, and Predicates

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Published
July 2021
Language
English (United States)
Last Update
2021-07-28
dita:mapPath
SQL_Functions__Expressions__and_Predicates.Upload_071421/djk1612415574830.ditamap
dita:ditavalPath
SQL_Functions__Expressions__and_Predicates.Upload_071421/wrg1590696035526.ditaval
dita:id
kby1472250656485

You want to create a histogram for the salaries of all employees whose salary amount ranges between $70000 and $200000. The width of each partition, or bucket, within the specified range is to be $32500.

The employee salary table contains eight employees:

salary   first_name   last_name
-------- ------------ -----------
50000    William      Crawford
150000   Todd         Crawford
220000   Bob          Stone
199999   Donald       Stone
70000    Betty        Crawford
70000    James        Crawford
70000    Mary         Lee
120000   Mary         Stone

You perform the following SELECT statement.

   SELECT salary, WIDTH_BUCKET(salary,70000,200000,4),COUNT(salary)
   FROM emp_salary
   GROUP BY 1Teradata Vantage
   ORDER BY 1;

The report produced by this statement looks like this.

salary   Width_bucket(salary,70000,200000,4)  Count(salary)
-------- ------------------------------------ ----------------
50000    0                                    1
70000    1                                    3
120000   2                                    1
150000   3                                    1
199999   4                                    1
220000   5                                    1