How to get ceil value with custom digit number in SQLite? - sql

In my database, I have prices of items, and the discount percents for the items.
Here is a sample row from the database
Name Price DiscountPercent
**** 1000.26 17.00
Now for this row I need to get the discounted price, but with two digits after the comma, i.e. for the discounted price 830.2158 I want to get 830.21
Doing
Round((UnitPrice - (UnitPrice * DiscountPercent)/100),2)
in my select query will return 830.22 which is not what I want. I could have achieved the desired result if I could do
Round(Floor((UnitPrice - (UnitPrice * DiscountPercent)/100) * 100)/100,2)
but unfortunately there is no Floor function in SQLite.
I also tried to cast to int and then divide by 100 and Round, but it seems that SQLite casts the whole result to int, because I get 830.0
How can I do this?
Thanks in advance

Ok this one is complicated and it is basically subtracting off the 3rd decimal place.
round((UnitPrice - (UnitPrice * DiscountPercent)/100)-(((UnitPrice - (UnitPrice * DiscountPercent)/100)%1*100)%1/100),2)
Easier to read and to get the basic Idea:
round(value -((value%1*100)%1/100),2)

SELECT cast(cast((UnitPrice - UnitPrice * DiscountPercent/100) * 100 as int) as real) / 100 FROM Product

Related

Want decimal result Amazon Redshift

I am trying to calculate the average ice creams a kid will have during summer.
I want the result to have 2 decimals.
Query:
Select day, (sum(ice_cream_cones) *1.0)/(select count(kids))
From t1 Group by 1
The result I get is something like 1.0003. I only want 2 decimal points.
Any suggestions?
You can do this with round
Select day, round((sum(ice_cream_cones) *1.0)/(select count(kids) ), 2)
From t1
Assuming your source data are integers...
SUM(ice_cream_cones) * 100 / COUNT(kids) / 100.0
Or, cast your existing calculation to a decimal?
CAST(<calc> AS DECIMAL(8,2))

How to extract a part of a decimal in Hive

I have two columns called quantity and price. Quantity is divided by price. If the result contains decimal, I want the number before the decimal. Or else, the number as it is.
I think you are looking for:
select floor(quantity / price) as output
Casting issues?
select cast(quantity / price as bigint)
By the way, I think you may want this:
Hive Data Types Manual
Also DIV operator can be used (for integer arguments):
with your_data as (
select stack(3, 13,2,8,2,233,2) as(quantity,price)
)
select quantity div price
from your_data d
;
Result:
6
4
116

SQL - 1. Round the difference to 2 decimal places

I am trying to create an SQL statement with a subquery in the SELECT attribute list to show the product id, the current price and the difference between the current price and the overall average.
I know that using the ROUND function will round the difference to zero decimals but I want to round the difference to 2 decimal places.
SELECT p_code, p_price, ROUND(p_price - (SELECT AVG(p_price) FROM product)) AS "Difference"
FROM product;
I tried using CAST but it still gave me the same output.
SELECT p_code, p_price, CAST(ROUND(p_price - (SELECT AVG(p_price) FROM Lab6_Product)) as numeric(10,2)) AS "Difference"
FROM lab6_product;
Thank you in advance for your time and help!
round() takes a second argument:
SELECT p_code, p_price,
ROUND(p_price - AVG(p_price) OVER (), 2) AS "Difference"
FROM product;
Note that I also changed the subquery to a window function.
I often recommend converting to a number or decimal/numeric) instead:
SELECT p_code, p_price,
cast(p_price - AVG(p_price) OVER () as number(10, 2)) AS "Difference"
FROM product;
This ensures that the two decimal points are displayed as well.

Calculate an average as a percent?

This is a simple problem and I'm surprised I couldn't find an answer to it.
All I need, is to calculate the average of a column and have the output as a percent.
Select round(avg(amount)::numeric, 2)
From Table
All I need is to figure out how to make that a percent, right now it comes out as 12345.67, how would I convert that output to a percentage in the query?
Maybe you need something like this:
select avg(amount) * 100 / sum(amount)
from Table
Or you can add a percent sign to it like this:
select CAST(avg(amount) * 100 / sum(amount)as VARCHAR(max)) + '%' AS Perc
from Table
or you can just concatenate a sign to your query:
Select CAST(round(avg(amount)::numeric, 2)as VARCHAR(max)) + '%' AS Perc
From Table
Result / Total * 100 = Average
In your case, the Result is: avg(amount)::numeric
and the Total is whatever the maximum that amount could be.
Your best bet would be to use whatever tools are available to you. But there is a way you can do it as a query.
Try getting the average divided by the sum multiplied by 100.
So you want the average, like below and divided by the sum, and multiplied by 100 to get 100% of the sum.
select avg(amount) / sum(amount) * 100

sql with rounding issue

I have a problem with sql(maths).
I have a total payable given to vendor which is 33.333, and I need to divide the amount with two users. So I have select
select (16.666 * 2) from dual which gives me 33.332 that is .1 less than the total amount I have to give.
If I have this sql
select (16.667 * 2) from dual, then it gives me 33.334 which .1 greater than 33.333.
How can I divide the total amount which I could equally distribute?
Thanks
I'm not sure from where are you executing your query, but it works here (SQLDeveloper, 10g):
SELECT (33.333 / 2) FROM dual;
16,6665
SELECT (16.6665 * 2) FROM dual;
33,333
Do it the other way around:
select 33.333/2
You are most likely working with the wrong column type. You should be using DECIMAL instead of e.g. FLOAT.
Here is a good summary: http://lists.mysql.com/mysql/189592
Depending on the SQL standard you are using the type can be MONEY, DECIMAL or NUMBER.