I have a Query that incorporates some calcualted fields. The 'Total' Column produces a value with 2 decimal places but the Commission and Net_Receipt Columns have 4 decimal places despite the code looking exactly the same format. Can anyone tell me why and how to correct the latter two to 2 DP's.
SELECT `exhibition_sales`.`name`,
`exhibition_sales`.`category`,
`exhibition_sales`.`catologue_number`,
`exhibition_sales`.`title`,
`exhibition_sales`.`quantity`,
`exhibition_sales`.`unit_price`,
`exhibition_sales`.`commision_rate`,
`exhibition_sales`.quantity * `unit_price` AS `Total`,
`exhibition_sales`.quantity * `commision_rate` * unit_price AS `Commision`,
`exhibition_sales`.quantity * `unit_price` - `quantity` * `unit_price` * `commision_rate` AS `Net_Receipt`
FROM exhibition_sales
ORDER BY `exhibition_sales`.`name` ASC
The rules for the output format of decimal places is very convoluted. If you want a particular number, then just cast to the format you want:
select . . .,
cast(`exhibition_sales`.quantity * `commision_rate` * unit_price as decimal(10, 2)) AS `Commision`,
cast(`exhibition_sales`.quantity * `unit_price` - `quantity` * `unit_price` * `commision_rate` as decimal(10, 2)) AS `Net_Receipt`
There is some explanation in the documentation.
Assuming you are on SQL-Server, the rules of multiplication and division with numerics apply:
Total is the multiplication of a (x, 0)-value with a (x, 2)-value, so the final scale will be 0+2 = 2. Hence 5 * 2.00 == 10.00
Commission is the multiplication of a (x, 0)-value, a (x, 2)-value and a (x, 2)-value so the final scale will be (0+2)+2 = 4. Hence 5 * 2.00 * 4.00 == 40.0000
Net_Receipt is the sum of two multiplications; the final scale will be max(s1 + s2, s3 + s4), that is max(0 + 2, 2 + 2) == 4. Hence 5 * 2.00 + 1.00 * 3.00 == 13.0000
Related
I have a dataset (in SQL) where I need to calculate the market value over the entire period. I want to automate this calculation in SQL. The Initial value is 2805.00 per month payable for 36 months. But the value is escalated at 5.5% after each block of twelve months. I have included a picture below to show the values and how they escalate. In terms of what fields I have in SQL, I have the length of the term (36 months [months]). I also have an escalation percentage (5.5% in this case [percentage]) and the starting value [starting_value], 2805.00 in this case. The total value (Expected Result [result]) is 106 635.72. I am after what the calculation should look like in SQL so that I can apply it across all market points.
Here is a fast performance formula to calculate the expected result [result] if you need to calculate every, annual salary with respect to the first one:
[result] = (N * (N + 1) / 2) * [increment] * 12 + M * (N + 1) * [increment] + [starting_value] * [months]
Where:
N = TRUNC([months] / 12) - 1// TRUNC --> Integer value of dividing [months] / 12
M = [months] MOD 12 // Rest of integer division of [months] / 12
[increment] = [percentage] * [starting_value] / 100
On the other hand, if you need to calculate each annual salary with respect to its predecessor you will need the formula below:
∑y=0TRUNC([months]/12)−1{([percentage]/100 + 1)y * 12 * [starting_value]} + ([percentage]/100 + 1)TRUNC([months]/12) + 1 * ([months] MOD 12) * [starting_value]
This is a bit confusing but there is no way to place formulas in Stack overflow. Moreover, if you need to run this in some DBMS you should assure someone that allows you to make loops, as the sum will need it.
You would need to adapt both formulas to the DBMS, taking into account the comments I placed before. Hope this to be helpful.
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.
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
I have this statement:
Select (30 - 5) * 700 / 30 as A , 700 - (5 * 700 / 30) as B
which has two ways to calculate the same equation.
These two equations should result in 583.33 if you made them by your calculator
but the previous statement results in 583 for field A, and 584 for field B.
Both are wrong and both are integer not decimal.
I want to know what's the right way to write this statement so I can get 583.33.
Thanks
Your expression is implicitly using an INT datatype. Try it this way to allow it to use a DECIMAL datatype
Select (30.000 - 5.000) * 700.000 / 30.000 as A ,
700.000 - (5.000 * 700.000 / 30.000) as B
Use Cast function:
As next:
Select Cast((30 - 5) * 700 as DECIMAL(19,2) ) / 30 as A ,
700 - ( Cast (5 * 700 as decimal (19,2 )) / 30) as B
Result:
A B
583.333333 583.333334
I'm basically trying to get the percent of two select counts. In the example shown below, I should have a simple 2 / 6 which results in 33.33%. However when I run the query is only returns either 100% or 0%. Any help on why it does what it does would be fantastic
CONVERT(DECIMAL(4, 2),
(SELECT Count(driver)
FROM trh a
WHERE a.hn = trh.hn
AND a.driver = trh.driver
AND (a.finishposition < 5
AND a.finishposition IS NOT NULL
AND a.finishposition != 0 )) / Count(driver) ) * 100
Now that I try CONVERT(DECIMAL(4, 2), 2 / 6), this just returns 0.00 also
Okay you have to cast them as decimals
SQL Server does integer division. Just cast() one of the values to a decimal or floating type. For instance:
/ cast(Count(driver) as decimal(4, 2))