/ ID: 805 Title: Use of Floor Function in Postgres SQL 805 Meta Description: This article teaches use of the Floor function in Postgres SQL. Code samples included. Meta Keywords: Floor Function Postgres SQL Author: orkb Template: Unstructured Tutorial Categories: PostgreSQL Tags: Floor, Function, PostgreSQL, SQL, Random, Generate_Series Status: Published /
In this tutorial, we learn to use the Floor function in our Postgres SQL queries. We'll begin easy and then add some complexity in order to best understand the scope and use of this function. Here we will explore:
The Postgres FLOOR() function returns the highest value integer that is equal to or smaller than the supplied number. In other words, it chops off the decimal point.
Syntax
FLOOR()
Did you notice anything strange from the above examples? Look again at the last two. Because of the other examples, we might think our output would be 2. However, when we remember the following rule about how Floor works, we'll see why our output for both should be -3:
"... returns the highest value integer that is equal to or smaller than the supplied number..."
Note: It's like the INT() function in many programming languages.
One way we may need to use the Floor function is when generating random numbers because Postgres' RANDOM function returns numbers that are many precision points below the decimal point. So we'll begin there.
First, let's take a quick look at how the FLOOR() and RANDOM() function work together.
``` SQL FLOOR(RANDOM()*10)
FLOOR(RANDOM()*10)+1
FLOOR(RANDOM()*(i_largest-i_smallest)+i_smallest) ```
Analysis
We want to fill a table with random numbers. We'll begin by creating a practice table called "test_random":
SQL
CREATE TABLE public.test_random (
id serial NOT NULL
, i_number int4 NULL DEFAULT 0
, i_random_number int8 NULL DEFAULT 0
, i_times_picked int8 NOT NULL DEFAULT 0
, d_time time NULL DEFAULT now()
);
CREATE UNIQUE INDEX test_random_id_idx ON public.test_random USING btree (id);
The SQL above should give you a table like this:
Analysis of the table we just created
SQL
INSERT INTO
test_random
(
i_number
, i_random_number
)
SELECT
i AS i_number
, FLOOR(RANDOM()*100)
FROM
generate_Series(1,10) AS i;
SQL Analysis
After executing the SQL above, your test_random table should have 10 rows and look just like this, except with different numbers in the "i_random_number" column:
OK. Yay, the numbers in our i_random_number column look to be random!
Now, we need to keep in mind we have generated only 10 numbers and really, that's not enough to know anything about how random our distribution is. Also, we don't have any duplicates, so no need to utilize our i_times_picked column yet. So let's generate more rows using the same query as before, except changing the line in our SQL that says generate_Series(1,10) AS i; to use "20" instead of "10".
SQL
INSERT INTO
test_random
(
i_number
, i_random_number
)
SELECT
i AS i_number
, FLOOR(RANDOM()*100)
FROM
generate_Series(1,20) AS i;
Now our data set has 30 records with random numbers. Take a look at your dataset. Are there any duplicate numbers in the i_random_number column? If not, run that INSERT query again. Keep running it until your resulting data set has at least one duplicate in the i_random_number field. If you are curious, you can set your "generate_series" function to create a few thousand rows. The more you create, the more likely we'll be to find any patterns that may exist
In this lesson, we learned how to use PostgreSQL's FLOOR() function to turn numbers into integers in SQL statements. We also used a real world example. We also worked with other functions, including RANDOM(), GENERATE_SERIES(), and COUNT().