SQL ending price with 5 - sql

I'm having a problem with a round.
I need to round prices to end with 5.
Example 1: 647,927 needs to end with 5 with tax. So it should be 815.
647,927 * 1,25 (tax) is 809,90 how can i round 809,90 up to 815
Example 2: 283,30 needs also to end with 5 with tax. So it should be 355.
283,30 * 1,25 (tax) is 354,125 how can i round 354,125 up to 355?

You can use arithmetic. Something like:
select ceiling( (val - 5) / 10 ) * 10 + 5
Here is a db<>fiddle for your two examples.

Related

I have divided 2 column in sql lite and received a output. However, the decimals are too long and I want only 2 decimals. How do I get that?

Below is the query which I have used. I need the result only 2 decimals not more than that.
:sum(CAST(CASE When t.issue_resolved like 'TRUE' then 1 else 0 END AS Float) * 100 /
count(t.issue_resolved) AS finalscore
t.issue_resolved (1 column)
2022-03-19 80.97826086956522 - (I want this to be only decimals)
You can use round function
round(sum(CAST(CASE When t.issue_resolved like 'TRUE' then 1 else 0 END AS Float) * 100 / count(t.issue_resolved),2) AS finalScore

How do you calculate using sum in SQL Server

I am trying something like this:
select sum(R_Value) ,((5/cast(sum(R_Value) as int)) * 100.000000000)
from R_Rating
where R_B_Id = 135
But I keep getting value 0, sum(R_Value) = 6, so I just want a percentage out of (5/sum(R_Value)) * 100.
How can I get this?
I have a rating of 5 so each user has a rating they can make select from 1 to 5, so what i need is a formula that takes in the sum and takes into account how many users have rated and give us a result from 1 to 5 in the end
Something like this may work but i need to round up to one decimal place to get a whole number.
select sum(R_Value), ((count(*)/cast(sum(R_Value) as float)) * 10)
from R_Rating
where R_B_Id = 135
To get the average rating you need to force floating point algebra. For example:
select 1.0 * sum(R_Value) / count(*)
from R_Rating
where R_B_Id = 135
Then, if your query selects three rows with the values: 1, 4, and 5, then this query will return 3.33 stars as the average. That is:
= 1.0 * (1 + 4 + 5) / 3
= 1.0 * 10 / 3
= 10.0 / 3
= 3.33333333
I recommend writing this as:
select sum(R_Value) ,
(500.0 / sum(R_Value))
from R_Rating
where R_B_Id = 135;
This avoids an integer division.

Looking to filter query based on last two digits in price column

I'm trying to run an analysis that looks at conversion changes based on price point. To do this, I am trying to bucket prices based on last two digits. How would i go about filtering based on the last two digits in the where clause? I'll want to filter prices ending in .99, (.25,50,.75,), .%0, etc.
Generally to filter by "last digits" one would use modulo operator %, that is rest of division.
For integers its meaning is clear: 123 % 100 = 23, 123 % 10 = 3. For any x being integer x % 1 = 0 (as any integer is divisible by 1)
And it seems in Redshift it works for decimals as well:
select 123.99 % 1 as cents;
cents
0.99
so select price from table where price % 1 = 0.99 should return you all prices ending with .99, which we can easily verify:
with prices as (
select 9.99 as price union
select 9.43 union
select 0.99 union
select 2
)
select *
from prices
where price % 1 = 0.99;
yields
price
9.99
0.99

SQL Roundup to 2 or 3 decimal

SQL only have round to nearest function and roundup to nearest number.
How can I round up my prices, if price <1 roundup to 3 decimal and if price >=1 roundup to 2 decimal.
E.g
Price
$0.0024 round up to 0.003
$0.0051 round up to 0.06
$1.213 round up to 1.22
Thanks.
Use ceiling() to always round up:
select case when price < 1 then ceiling(price * 1000) / 1000
else ceiling(price * 100) / 100
end
from table
you can use round function with case below will work for sql server
select case when price<1 then round(price,3) else round(price,2) end
from table
There is 2 things to know :
ROUND() Always round DOWN :
ROUND(0.051, 2) = 0.05
If you want to round up, you need to use another function : CEILING()
But Ceiling only return an integer. The tricks is to do this :
CEILING(0.0051 * 1000)/1000 = 0.006
For the data you provide here is the solution :
SELECT
CASE
WHEN price < 1 THEN CEILING(price*1000)/1000
ELSE CEILING(price*100)/100
END AS rounded_price_up
FROM mytable

Why is this SQL SUM statement correct?

In my schema, I have a table Projects, and a table Tasks. Each project is comprised of tasks. Each task has Hours and PercentComplete.
Example table:
ProjectID TaskID Hours PercentComplete
1 1 100 50
1 2 120 80
I am trying to get the weighted percentage complete for the project. I am doing this using the following SQL statement:
SELECT P.ProjectID, P.ProjectName, SUM(T.Hours) AS Hours,
SUM(T.PercentComplete * T.Hours) / 100 AS CompleteHours,
SUM(T.PercentComplete * T.Hours) / SUM(T.Hours) AS PercentComplete
FROM Projects AS P INNER JOIN
Tasks AS T ON T.ProjectID = P.ProjectID
WHERE (P.ProjectID = 1)
My question is about this part of that statement:
SUM(T.PercentComplete * T.Hours) / SUM(T.Hours) AS PercentComplete
This gives me the correct weighted percentage for this project (in the case of the sample data above, 66%). But I cannot seem to wrap my head around why it does this.
Why does this query work?
SUM(T.PercentComplete * T.Hours) / 100 is the number of complete hours.
SUM(T.Hours) is the total number of hours.
The ratio of these two amounts, i.e.:
(SUM(T.PercentComplete * T.Hours) / 100) / SUM(T.Hours)
is the proportion of hours complete (it should be between 0 and 1).
Multiplying this by 100 gives the percentage.
I prefer to keep percentages like this out of the database and move them to the presentation layer. It would be much easier if the database stored "hours completed" and "hours total" and did not store the percentages at all. The extra factors of 100 in the calculations confuse the issue.
Basically you are finding the number of hours completed over the number of hours total.
SUM(T.PercentComplete * T.Hours) computes the total number of hours that you have completed. (100 * 50) = 50 * 100 + (120 * 80) = 146 * 100 is the numerator. 146 hours have been completed on this job, and we keep a 100 multiplier for the percent (because it is [0-100] instead of [0-1])
Then we find the total number of hours worked, SUM(T.Hours), which is 100 + 120 = 220.
Then dividing, we find the weighted average. (146 * 100) / 220 = 0.663636364 * 100 = 66.4%
Is this what you were wondering about?
It calculates the two sums individually by adding up the value for each row then divides them at the end
SUM(T.PercentComplete * T.Hours)
50* 100 +
80 * 120
-------
14,600
SUM(T.Hours)
100 +
120
---
220
Then the division at the end
14,600 / 220
------------
66.3636
Edit As per HLGEM's comment it will actually return 66 due to integer division.
Aggregate functions, such as SUM(), work against the set of data defined by the GROUP BY clause. So if you group by ProjectID, ProjectName, the functions will break things down by that.
The SUM peratiorn first multiply the columns than add
( 100* 50+ 120* 80) / (100+ 120)