I have a table with a decimal field in Postgres
The value of the field is 39.95
I try to make a SQL-Query to get all records with value 39.95
This is my code:
SELECT CR_Sale.id , CR_Sale.sale_date, ROUND(CR_Sale.sale_bruto_total, 2)
AS sale_bruto_total, CR_Sale.sale_ticket
FROM crm_CR_Sale CR_Sale
WHERE sale_bruto_total = 39.95
ORDER BY sale_timestamp DESC;
When I execute the query no results are found.
If I change my code to
WHERE sale_bruto_total <= 39.95
I get a result and can see the records with value 39.95
What am I doing wrong?
Thanks
Try as follow:-
SELECT CR_Sale.id , CR_Sale.sale_date, ROUND(CR_Sale.sale_bruto_total, 2)
AS sale_bruto_total, CR_Sale.sale_ticket
FROM crm_CR_Sale CR_Sale
WHERE sale_bruto_total = CAST(39.95 AS dec(5,2))
ORDER BY sale_timestamp DESC;
Note:- 'sale_bruto_total' column should be decimal type
The number 39.95 is not a decimal value but a float and then you can't make equality comparisons due to the internal precision of floats (this being the reason why there is the decimal data type in the first place) so you have to add an explicit cast:
sale_bruto_total = 39.95::decimal(5,2)
Also, if the value of sale_bruto_total is exactly 39.95 because the field is defined as a decimal with two decimals after the dot, then you do not need the round() function in the select list. On the other hand, if there are more decimals then you need to use the round() function in the filter too. So either:
SELECT id, sale_date, round(sale_bruto_total, 2) AS sale_bruto_total, sale_ticket
FROM crm_CR_Sale
WHERE round(sale_bruto_total, 2) = 39.95::decimal(5,2)
ORDER BY sale_timestamp DESC;
or
SELECT id, sale_date, sale_bruto_total, sale_ticket
FROM crm_CR_Sale
WHERE sale_bruto_total = 39.95::decimal(5,2)
ORDER BY sale_timestamp DESC;
Related
I have a rounding length value by contract. I would like to round my data table by this value however I cannot find how to input it dynamically (ie changing 2 in the example below by 5). Is it possible?
Table trading.data18 structure
idcontract integer
close double
Table contracts structure
idcontract integer
rounding double
My static query so far
select ROUND(CAST(close AS numeric),2) from trading.data18 limit 10;
You can use contracts.rounding as a second argument of round.
select round(d.close, c.rounding::integer)::numeric
from trading.data18 as d
join contracts as c using(idcontract);
using(idcontract) is a shorthand for on c.idcontract = d.idcontract
A very simple query which work
select avg(price) from cars where type = 'electric';
the result is
1
---------------------------------
45000,00000000000
I want to remove the ,00000000000 to obtain
1
---------------------------------
45000
I have tried cast and round but without success.
I'm on Db2
This nice ltrim works fine for all type of cars
select replace(ltrim(replace(price,'0',' ')),' ','0') from cars;
but using with avg return the string with the 00000
select replace(ltrim(replace(avg(price),'0',' ')),' ','0') from cars where type = 'electric';
the result is..
45000.00000000000
:(
Only the , is changed.
casting this value should just work:
select cast(avg(price) as int) as avg_int
from cars
where type = 'electric';
Casting as an integer as suggested by another answer will work.
However, keep in mind that
you'll be truncating any decimal values.
you're converting from one type to another.
You can resolve #1 by casting after ROUND()
select int(round(avg(price),0)) as avg_int
from cars
where type = 'electric'
You can resolve #2 by using DECIMAL() or ZONED() with zero for scale depending on the original column type.
select dec(avg(price),10,0) as avg_dec
from cars
where type = 'electric'
And of course ROUND() could be used with DECIMAL() or ZONED() also...
I have a table with schema as below
fields. TIME_256Hz TIMESTAMP REPEATED
fields. SAC_ACCELX FLOAT REPEATED
fields. SAC_ACCELY FLOAT REPEATED
fields. SAC_ACCELZ FLOAT REPEATED
fields. SAC_GYROX FLOAT REPEATED
fields. SAC_GYROY FLOAT REPEATED
fields. SAC_GYROZ FLOAT REPEATED
fields. SAC_PRESSURE FLOAT REPEATED
fields. TARGET STRING REPEATED
The table looks like
I try to sort the data with timestamp using this command
select * from liftpdm.liftpdm_2.acc8
order by fields.TIME_256Hz desc
BUT
I keep getting the following error:
ORDER BY does not support expressions of type ARRAY<TIMESTAMP> at [2:10]
I tried to change the Datatype to Datetime, string but same error.
Please suggest how to sort out this data
Thanks
Reproducible error:
WITH data AS (
SELECT [1,2] arr UNION ALL
SELECT [2,1] UNION ALL
SELECT [4,5,6]
)
SELECT * FROM data
ORDER BY arr
# ORDER BY does not support expressions of type ARRAY<INT64> at [8:10]
First you need to decide what do you really want to ORDER BY, as ordering by an array doesn't make much sense.
Some alternatives:
SELECT * FROM data
ORDER BY arr[SAFE_OFFSET(0)]
SELECT * FROM data
ORDER BY arr[SAFE_OFFSET(1)]
SELECT * FROM data
ORDER BY ARRAY_LENGTH(arr) DESC
I'm using a Firebird database and am trying the following sql but each time it returns 0, instead of 0.61538 (etc).
SELECT (COUNT(myfield)/26) totalcount
FROM mytable
Now when I remove the /26, the totalcount returns 16 as it should. But when I add the divided by 26 back in, the result shows as 0, but it should show as the full decimal value of 0.615384... Does anyone know why it's not returning the full value? I've even tried wrapping it in a CAST((count(myfield)/26) as double) totalcount but it still returns 0.
Thank you in advance for any suggestions!!!!
Try:
SELECT COUNT(myfield/26.0) totalcount
FROM mytable
Or:
SELECT COUNT(CAST(myfield as double)/26) totalcount
FROM mytable
Not familiar with Firebird, but in other implementations you have to cast/convert either the numerator or denominator as a decimal before division, as integer division returns an integer value.
I have one table named MemberCheque where the fields are:
MemberName, Amount
I want to to show the name and the respective amount in numbers and as well as in words after separating the integer amount from the decimal. So my query is like:
SELECT MemName, Amount, (SELECT (Amount)%1*100 AS lefAmn, dbo.fnNumberToWords(lefAmn)+
'Hundred ', (Amount) - (Amount)%1 AS righAmnt, dbo.fnNumberToWords (righAmnt)+' Cents'
from MemberCheque) AS AmountInWords FROM MemberCheque
but my store procedure can take only integer value to change into words. So, I am doing separating the Amount into two parts before and after decimal but when I am trying to run this query it gives me error that lefAmn and righAmnt is not recognised. Because I am trying to send the parameter from the same query.
The first problem is that you have a subquery that is returning more than one value, and that is not allowed for a subquery in the select clause.
That answer to your specific question is to use cast() (or convert()) to make the numbers integers:
select leftAmt, rightAmt,
(dbo.fnNumberToWords(cast(leftAmt as int))+'Hundred ' +
dbo.fnNumberToWords(cast(rightAmt as int))+' Cents'
) as AmountInWords
from (SELECT (Amount%1)*100 AS leftAmt,
(Amount) - (Amount)%1 AS rightAmt
from MemberCheque
) mc
If you can't alter your function, then CAST the left/right values as INT:
CAST((Amount)%1*100 AS INT) AS lefAmn
CAST((Amount) - (Amount)%1 AS INT) AS righAmnt
You can't pass the alias created in the same statement as your function parameter, you need:
dbo.fnNumberToWords (CAST((Amount)%1*100 AS INT))
dbo.fnNumberToWords (CAST((Amount) - (Amount)%1 AS INT))