Get difference between two numbers in WHERE statement? - mysql

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.

Related

Doing arithmetic on the results of a MySQL select command

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

SELECT not returning anything using HAVING clause

I'm trying to get the total percentage off and only return the matches >+ 80. However, this doesn't return any results:
SELECT * FROM products WHERE Available=1 AND Merchant='Amazon' HAVING (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?
Am I using HAVING correctly?
HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.
HAVING is applied after the aggregation phase and must be used if you want to filter aggregate results.
Your query is wrong.
What you can do is do the conditioning in where clause only.
SELECT *
FROM products
WHERE Available=1
AND Merchant='Amazon'
AND (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?
As per I understand you wanna use having to filter whereas you may use just where condition.
About returning results, your query will produce syntax error. If you use as following and don't get any result then obviously it is because of conditions and your data. In that case if you provide data, you may get some help.
SELECT * FROM products
WHERE Available=1
AND Merchant='Amazon'
AND (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?
SELECT *, (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 as percentage FROM products WHERE Available=1 AND Merchant='Amazon' HAVING percentage >= ?
Your main problem is that your percentage formula is wrong. Use
((LowestUsedPrice - LowestNewPrice) / LowestNewPrice) * 100
or
(LowestUsedPrice - LowestNewPrice) * 100 / LowestNewPrice
The way you are using will do something like
(20 - 8) / 15 * 100
12 / 1500 = 0.008
And you need
1200 / 15 = 80
Add this fix to the solution on other answers

Criteria on values taken into consideration in a MYSQL MIN/Max/Avg calculation

I am trying to get the MIN/MAX/AVG from my database, but there may be some spurious results from time to time. Basically, I only want MYSQL to give me the minimum etc of a difference calculation.
The criteria I need would be +65.00 each way.
Here is some sample data
Actual
1854
1843
1865
1822
1833
1859
1400
Here is my query -
Select MIN(ACTUAL - 1800), MAX(ACTUAL - 1800), AVG(ACTUAL - 1800) FROM ACTUAL_TABLE WHERE DATE = '2015-08-09'
This query result would be -
MIN MAX AVG
-400 65 -17.71428571
So clearly, the query is picking up the 1400 in the actual table, and the difference is -400, I need it to ignore this result as its past the > -65.00 criteria I need.
If the criteria was in place the result would look like this -
MIN MAX AVG
22 65 -17.71428571
I have tried putting a CASE in the SQL and had no joy. Can anyone shed some light on this issue?
Thankyou.
Select only the values you want
Select MIN(ACTUAL - 1800), MAX(ACTUAL - 1800), AVG(ACTUAL - 1800)
FROM ACTUAL_TABLE
WHERE DATE = '2015-08-09' AND ACTUAL >= (1800-65) AND ACTUAL <= (1800+65);

MySQL how to select the nearest values to my variables

I have the following query where Im trying to retrieve matches, within a certain breathing space, of the variables entered.
SELECT fthg, ftag, avover, avunder, whh, wha, whd
FROM full
WHERE (whh < ($home_odds + 0.05)
AND whh > ($home_odds - 0.05)
AND wha < ($away_odds + 0.05)
AND wha > ($away_odds -0.05)
AND whd < ($draw_odds + 0.05)
AND whd > ($draw_odds - 0.05))
There are occasions where this returns 0 results so in that case I would like to retrieve the closest matching record to all three but Im not quite sure how to put the query together.
Basically this is the last resort if the other query doesn't return results, this one will return the next best thing no matter how far from the original values.
Thanks for the help
Your original query would be simpler and more readable as this:
SELECT
fthg,
ftag,
avover,
avunder,
whh,
wha,
whd
FROM full
WHERE ABS($home_odds - whh) < 0.05
and ABS($away_odds - wha) < 0.05
and ABS($draw_odds - whd) < 0.05
If that query returns nothing, you could run this one:
SELECT
fthg,
ftag,
avover,
avunder,
whh,
wha,
whd
FROM full
ORDER BY
ABS($home_odds - whh) + ABS($away_odds - wha) + ABS($draw_odds - whd)
LIMIT 1
It will return the row with the lowest deviation from the combination of those three pairs of fields.
How about faking a distance calculation between the parameters you provide and the parameters you are comparing to? Something like
SELECT fthg, ftag, avover, avunder, whh, wha, whd
FROM full
ORDER BY
sqrt(abs(whh - $home_odds) * abs(whh - $home_odds)) +
sqrt(abs(wha - $away_odds) * abs(wha - $away_odds)) +
sqrt(abs(whd - $draw_odds) * abs(whd - $draw_odds))
This way, even if there are no matches given the range you are interested in, you can still get a closer result.

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