I have a query in r, using rmysql package :
sql.query = paste0("SELECT * FROM performance_db.pcTab WHERE
fk_ID_calc = (SELECT ID_calc FROM performance_db.calculationTab WHERE
turbine_model = '",input$t1,"' AND
op_mode = '",input$t2,"' AND
date_entered ='",input$t3,"')
AND sensor = 'P_el'"
)
spc = fetch(dbSendQuery(conn, sql.query), n = -1)
this return a table :
head(spc)
ID_pc voltage_side sensor Air_density v2.5 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19
1 1 0 P_el 1.225 0 44 184 404 721 1165 1686 2107 2272 2300 2300 2300 2300 2300 2300 2300 2300 2300
2 2 0 P_el 1.000 NA 33 155 346 622 1010 1486 1967 2224 2286 2294 2300 2300 2300 2300 2300 2300 2300
3 3 0 P_el 1.030 NA 35 161 358 642 1041 1529 1999 2235 2291 2297 2300 2300 2300 2300 2300 2300 2300
4 4 0 P_el 1.060 NA 37 167 369 661 1072 1571 2036 2249 2295 2300 2300 2300 2300 2300 2300 2300 2300
5 5 0 P_el 1.090 NA 39 173 381 681 1103 1612 2060 2256 2300 2300 2300 2300 2300 2300 2300 2300 2300
6 6 0 P_el 1.120 NA 42 178 392 701 1134 1649 2080 2263 2300 2300 2300 2300 2300 2300 2300 2300 2300
I would like to add additional filter on Air_density like :
sql.query = paste0("SELECT * FROM performance_db.pcTab WHERE
fk_ID_calc = (SELECT ID_calc FROM performance_db.calculationTab WHERE
turbine_model = '",input$t1,"' AND
op_mode = '",input$t2,"' AND
date_entered ='",input$t3,"')
AND sensor = 'P_el' AND Air_density = 1"
)
This is return just one row contain the information for Air_density = 1 :
ID_pc voltage_side sensor Air_density v2.5 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19
1 2 0 P_el 1 NA 33 155 346 622 1010 1486 1967 2224 2286 2294 2300 2300 2300 2300 2300 2300 2300
But when I try to filter base on other Air_densities It does not return anything. Like if in above mentioned query I put AND Air_density = 1.225
:
head(spc)
[1] ID_pc voltage_side sensor Air_density v2.5 v3 v4 v5 v6
[10] v7 v8 v9 v10 v11 v12 v13 v14 v15
[19] v16 v17 v18 v19
I have no idea why it is happening !!!
FLOAT
and DOUBLE
data types are not precise.
If you save the value 1.225
it can be saved as 1.22500001
.
And when you try to query all data with the value 1.225
, it won't find it, because it is saved as 1.22500001
.
You should better use datatypes DECIMAL
or NUMERIC
or try to CAST
the FLOAT
values as DECIMAL
like this:
CAST(Air_density as DECIMAL(10,3))
This is from the docs of MySQL:
11.2.3 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE
The FLOAT and DOUBLE types represent approximate numeric data values. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
For FLOAT, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT in parentheses. MySQL also supports this optional precision specification, but the precision value is used only to determine storage size. A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column.
MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.
Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.5.4.8, “Problems with Floating-Point Values”
For maximum portability, code requiring storage of approximate numeric data values should use FLOAT or DOUBLE PRECISION with no specification of precision or number of digits.
https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html
This link should help you, too:
https://dev.mysql.com/doc/refman/5.7/en/problems-with-float.html