Which gives us back the following results: t_name_tech , ROUND (i_rating, 2 ) AS i_rating_round Now we modify our query, specifically, what precision we are asking of the ROUND function, changing the “1” to a “2”: What if we were to round, but with a precision of 2 instead of 1? First, let’s start out with our original data: id_tech So 14.38 turns into 14.4 because the 8 here is greater than 5. Notice in the row where the t name_tech column has a value of “Python”, how the Round() function turned 14.382 into 14.4? That’s because Round() looks at the number after our _designated decimal cut-off position, 1 in this case, and asks, “Is this number equal to or greater than 5?” If the answer is yes, then the preceding number, at position 1, is incremented by one. FROM: For the above commands, use the table called “technologies” as the source of our query data.Įxecuting the SQL query above returns the following recordset of data: t_name_tech.ROUND: This tells PostgreSQL to take the number in the i rating column, round it up to 1 decimal, and name the result _AS i_rating_round.SELECT: We chose two columns to return from our table t_name_tech and i_rating_round., ROUND (i_rating, 1 ) AS i_rating_round The twist: we want to have that number we pull to be consistently in the format of #.#, i.e., having precision to only one decimal. We’ll now write some SQL that pulls a rating of each technology from the i_rating field/column of the technologies table above. Let’s begin with some sample data, a table called technologies: id_tech Now we’ll play with the ROUND function in a database environment using SQL so as to best understand how we can incorporate use of the ROUND function into our Postgres-based projects. If none is supplied, Postgres assumes zero. Note here that the precision parameter above is optional. By “precision” here, we mean a certain number of decimal places. The PostgreSQL ROUND function returns a number after rounding the initial number to a certain precision. What is this ROUND function and how does it work? These include CEILING(), FLOOR(), and TRUNC().
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |