Doing arithmetic on the results of a MySQL select command - mysql

I have a MySQL database with temperatures stored in Centigrade. Assuming my select command is:
Select Temp1, temp2, temp3 from TableA where ....
Is there a way in the Select statement to convert the output to Fahrenheit, ie report Temp1*9/5+32 instead of Temp1?
Thanks....RDK

Is there a way in the Select statement to convert the output to Fahrenheit, ie report Temp1*9/5+32 instead of Temp1?
Yes. It's about as easy as it looks. How about this?
SELECT (Temp1 * 9.0 / 5.0) + 32.0 TempF
Or to avoid messy long decimal numbers in the result, this.
SELECT ROUND((Temp1 * 9.0 / 5.0) + 32.0, 1) TempF

Related

Convert SQL query into MYSQL

CONVERT(nvarchar(4), t/60) + '.' + CONVERT(nvarchar(4), t % 60) as t
Select CONCAT(FLOOR(t / 60), '.', MOD(t, 60)) as t
from (SELECT avg(TIMESTAMPDIFF(second,tur.start,tur.[end])) as t
FROM tblUserTracking tur where tur.start
between DATE_FORMAT(p_FromDt,'%m/%d/%Y')
and TIMESTAMPADD(DAY,1,DATE_FORMAT(p_Todt,'%m/%d/%Y')) ) as tbl1
In MySQL, use CONCAT():
CONCAT(FLOOR(t / 60), '.', MOD(t, 60)) as t
I assume t is an integer and you want integer division.
It looks like you are trying to format a time. If so, your code doesn't look right in either database. I might suggest that you ask a new question with sample data and desired results. In particular, this will product '5.1' and '5.10' for 301 seconds and 310 seconds respectively. Those look about the same to most people.
you can use this link to convert query
http://www.sqlines.com/online

SQL use FORMAT() on a SUM()

I want to use FORMAT on SUM to produce a currency style format with commas and no decimal places. I'm trying the following using MySQL 5.7:
SELECT FORMAT(SUM(x.07_17 / fx.07_17), 0) AS total.....
The problem is this changes the data drastically. Without the format, I get the correct result of SUM 350914 but with the format, in place, I get 350, so just the first 3 numbers.
What is it I'm doing wrong?
You can check the documentation to see how each parameters works for the FORMAT function https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_format
This query shows you different style to display your data (Sql fiddle example)
SELECT SUM(c1 / c2) AS total1
, CONCAT(FORMAT(SUM(c1 / c2), 3, 'fr_FR'), ' €') AS total2
, CONCAT('$', FORMAT(SUM(c1 / c2), 3)) total3
FROM ( SELECT 148277 c1
, 5.1561 c2 ) t

Get difference between two numbers in WHERE statement?

I'm trying to get the difference between two numbers (one of the values in a column and one arbitrary one) and then check if the difference is less than .20 in a WHERE statement. Here is what I have so far:
SELECT * FROM `products` WHERE (`lowest_price` - 5) >= .20 OR (5 - `lowest_price`) <= .20
(where 5 is the arbitrary number, and lowest_price is the column I'm comparing it to)
However, when I run this statement, I get results that I'm not expecting. What am I doing wrong?
The SQL statement is wrong
Try SELECT * FROM products WHERE (lowest_price - 5.0) BETWEEN -0.2 and 0.2
You could try SELECT (lowest_price - 5.0), * FROM products WHERE (lowest_price - 5.0) BETWEEN -0.2 and 0.2 which will help you where you are going wrong.

mysql get difference from the result of 2 fields queried

I'm not good at sql but I can create,understand common SQL queries. While scouring the net it seems its hard to find a befitting way on this query.
I have a query which is
SELECT COUNT(`BetID`),
FORMAT(SUM(`BetAmount`),0),
FORMAT(SUM(`Payout`),0),
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
I would like to subtract the result of
FORMAT(SUM(`BetAmount`),0)
and
FORMAT(SUM(`Payout`),0)
Any other ideas to execute subtraction in this mysql query?
If you want the numbers rounded before subtracting them (which seems to be the case when you want to subtract the formatted numbers), you'll need to round them first to the same precision as the formatting, subtract and lastly format the result;
SELECT COUNT(`BetID`),
FORMAT(SUM(`BetAmount`),0),
FORMAT(SUM(`Payout`),0),
FORMAT(ROUND(SUM(`BetAmount`),0) - ROUND(SUM(`Payout`),0),0) diff,
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
A simple SQLfiddle to test with.
Use FORMAT((SUM(BetAmount) - SUM(Payout)),0)
Try this:
SELECT COUNT(`BetID`),
FORMAT(SUM(`BetAmount`),0),
FORMAT(SUM(`Payout`),0),
FORMAT((SUM(`BetAmount`) - SUM(`Payout`)),0),
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
You could also try using a join statement so that the calculation is only done once:
SELECT *,t.BetTotal - t.PayoutTotal as Difference
FROM (
SELECT
COUNT(`BetID`) AS Count,
FORMAT(SUM(`BetAmount`),0) as BetTotal,
FORMAT(SUM(`Payout`),0) as PayoutTotal,
ROUND((SUM(`BetAmount`) / COUNT(`BetID`)),2),
ROUND((((SUM(`BetAmount`) + SUM(`Payout`)) / SUM(`Payout`)) * 100),2)
FROM `betdb`
) as t

Is any way to convert decimal to time in MySQL?

I created a field called 'hours_spent' in MySQL using the decimal datatype to store time. The values are stored like this 1.30, 2.30 etc... (for 1hr30min, 2hr30min).
I want to calculate the sum of various time values.
The sum of time is not what I expected: 1.30 + 2.30 = 3.60, whereas I expected 4.00.
I used the SUM function in MySQL to count the hours_spent field. If the values are 0.30 + 1.50 = 1.80, whereas I expected 2.20.
My first mistake was to use the decimal type instead of the time datatype, but I cannot change datatype.
So, is there any way to sum the time values and get result as I expect?
Thanks
I prepared you a demo at sqlfiddle, you can try it there if you want:
http://www.sqlfiddle.com/#!2/c9afc/2
Here are the query samples:
select #indexer:=instr(dateasdecimal, '.')
, left(dateasdecimal, #indexer-1) * 60 + substr(dateasdecimal, #indexer+1) as totalMinutes
from testtable;
select #indexer:=instr(dateasdecimal, '.')
, sum(left(dateasdecimal, #indexer-1) * 60 + substr(dateasdecimal, #indexer+1)) as totalMinutes
from testtable;
Note: Please don't forget to accept answers to your questions:
https://meta.stackexchange.com/a/65088/200585
To convert a decimal into seconds, you could use this:
truncate(hours_spent,0)*60+(hours_spent-truncate(hours_spent,0))*100
and then you can do the sums easily. Then you can convert back seconds to the decimal format with this:
truncate(seconds/60,0)+truncate(mod(seconds, 60)/100,2)
You could always turn the decimals into a string, cast as time, then sum that time using time_to_sec and produce a formatted time with sec_to_time. Of course, it would be much better to be storing those times a different way, even if it involves converting the entire dataset.
SELECT sec_to_time(sum(time_to_sec(goodTime))) FROM (
SELECT CAST(badTime AS TIME) AS goodTime FROM (
SELECT REPLACE(badTime, '.', ':') AS badTime FROM (
SELECT CAST(badTime AS dec(4,2)) AS badTime FROM (
SELECT 1.3 AS badTime
UNION select 2.3
) z
) y
) x
) w