/ 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 /

Introduction

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:

Prerequisites

What is the FLOOR() function and how does it work?

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.

Why and when would we use the FLOOR() function?

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

Now let's begin building a mini-project

Our target goal

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:

Floor function Postgres SQL

Analysis of the table we just created

Step 2. Fill our new table with data

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:

Table after insert of Random integers

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".

Step 3: Add 20 more rows to our 10-row dataset.

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

Conclusion

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().