I've been reading threads and seeing the 'Convert' and 'Cast' commands, but I cannot get any to fit in this simple select statement. Very frustrating to have to ask a question this simple, but I cannot find anything that works. Below is the code, and I need the 'discount amount/price' to show up with 2 decimal places. Thank you in advance.
select product_name, list_price, discount_percent, list_price *
(discount_percent*.01) as 'discount_amount',
list_price - (list_price * (discount_percent*.01)) as 'discount_price'
from products
order by discount_price desc
limit 5;
The database should give you back a decimal/numeric type that is capable of storing the fractional digits, but the results grid and/or your application is still responsible for how to display those digits.
Your query looks like it does implicit casting, but you can explicitly do it like this:
select
product_name, list_price, discount_percent, list_price *
(cast(discount_percent as decimal(18,2)) * .01) as 'discount_amount',
cast(list_price as decimal(18,2)) - (cast(list_price as decimal(18,2)) * (cast(discount_percent as decimal(18,2)) * .01)) as 'discount_price'
from products
order by discount_price desc
limit 5;
Your displayed query results might still do its own rounding and display, for example "5" instead of "5.00", but that is because your UI still needs to implement it's own logic to display whatever format you're looking for.
In MySQL, you can use round or truncate:
SELECT ROUND(12.545, 2)
SELECT TRUNCATE(12.545, 2)
in MS SQL, you can Convert:
SELECT CONVERT(DECIMAL(10,2), ColumName) FROM TableName
Related
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
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.
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
I feel like I'm missing something obvious. I am trying to test out the distribution of random(). Here is the table:
create table test (
id int,
random_float float,
random_int int
);
Here is what I want to do:
truncate table test;
insert into test (id)
values (generate_series(1,1000));
update test
set
random_float = random() * 10 + 1;
update test
set
random_int = trunc(random_float);
select
random_int,
count(random_int) as Count,
cast( count(random_int) / max(id) as float) as Percent
from test
group by random_int
order by random_int;
However, the "Percent" column returns zero for every record. I tried casting it as float, as decimal, I tried changing the random_int column to decimal instead of integer, always same result.
Here is a fiddle.
Any insight as to what I am doing wrong?
You should cast before you divide, but also you were missing a subquery to get the total count from the table. Here's the sample.
select
random_int,
count(random_int) as Count,
cast(count(random_int) as decimal(7,2)) / cast((select count(random_int) from test) as decimal(7,2)) as Percent
from test
group by random_int
order by random_int;
Try this query instead:
select
random_int,
count(random_int) as Count,
cast( count(random_int) / max(id) as float) as Percent,
(100.0 * count(random_int) / max(id))::numeric(5,2) as pct
from test
group by random_int
order by random_int;
PostgreSQL has a strong types system. In your case, type is implied by count() function, which returns bigint (or int8) and id column, which is integer.
I would recommend using 100.0 as initial multiplier, that'll cause whole expression to be calculated as numeric and will also provide real percents. You might also want to cast to numeric(5,2) at the end to get rid of too big number.
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.