Johnathon22 Johnathon22 - 5 months ago 6x
Ruby Question

Returning SQL as decimal

I use Dashing as the project with Ruby.

I have a SQL query that returns

when run in the database. However, when I put it into code it returns
. What I am trying to figure out is how to get the decimal point to move right by two places.

Here is what the query and code look like:

require 'mysql2'

SCHEDULER.every '2h', :first_in => 0 do |job|

# MySQL connection
db = => "host", :username => "username", :password => "password", :port => port, :database => "database" )

sql6 = "SELECT AVG(tickets.total_user_waiting) / (60 * 60) AS 'Total Tickets' FROM tickets WHERE tickets.status IN ('resolved', 'archived') AND tickets.date_resolved between date_format(date_sub(now(), interval 7 day), '%Y-%m-%d 00:00:00') and date_format(date_sub(now(), interval 1 day), '%Y-%m-%d 23:59:59');"
response_sql = db.query(sql6)

send_event('response_sql', {current: response_sql.first['Total Tickets']})

Here is the result of the SQL query alone from the database:

| 19.40648160 |

The output of the code that is written in Ruby looks like:


I'm not so much worried about removing the end numbers as I am just getting the decimal two places to the right, although that would be ideal.

I've tried select round but had no luck with that in the database.


The solution that fixed the issue I was having is.

send_event('response_sql',    {current: response_sql.first['Total Tickets'].ceil})

So what I did was added .ceil to the end of the send event. It then rounded it to just two numbers.