Code Not calculating correctly Cast /NullIF - sql

I’m trying to calculate CNC machine efficiency. My code doesn’t calculate correctly. I’m not sure why. For instance, The Completed value is 2. The PPHOURGOAL is 1. The 8 is for an 8-hour shift.
cast(((Completed / 8) /NULLIF(Completed,0) / (PPHOURGOAL * 8) )* 100 as dec(5,4)) as EightHourShiftEfficiency, ---Divide the previously calculated production rate by the maximum rate and multiply by 100 to calculate the efficiency rate.
Calculation should be as ((2/8)/(1*8))*100 = 2. Which should translate to ((.25)/(8))*100=2
But the system is returning 1.5625 instead.

I am guessing that integer division is the culprit. Does this do what you want?
cast(((Completed / 8.0) /NULLIF(Completed, 0.0) / (PPHOURGOAL * 8.0) ) * 100.0 as dec(5, 4)) as E

Found the error of my ways. My select statement was doubling rows that caused my calculations to be off.

Related

BASIC Questions: Using ROUND for calculations

I am in Google's Data Analytics course and every now and then I get to a spot where I could just press on, but I want to know the answer anyway.
We are learning embedded calculations and a very simply equation was given:
`
SELECT
Date,
Region,
Total_Bags,
Small_Bags,
(Small_Bags / Total_Bags) * 100 AS Small_Bags_Percent
FROM `shaped-fx-370703.avocado_data.avocado_prices`
WHERE
Total_Bags <> 0
`
Resulting column from calculation
I would like to know how to use ROUND so that the result in Small_Bags_Percent only go out to 1 decimel
I understand the syntax of round is ROUND(integer,decimel places) but I don't know how to point the "integer" to the results in Small_Bags_Percent. I've tried THEN and even
ROUND(((Small_Bags / Total_Bags) * 100 AS Small_Bags_Percent),2)
``` (hey, I'm just seeing what sticks lol)
The ROUND function goes round the calculation but not the alias for the calculation e.g.
ROUND((Small_Bags / Total_Bags) * 100,2) AS Small_Bags_Percent

Rails Decimal Calculations with funky output

I'm trying a few decimal calculations, in a Rails app, but am getting some weird results.
#pprice = item.price - (( item.price / 100 ) * promo.discount)
#pprice = item.price - promo.discount
Each value (item.price, promo.discount etc) is a decimal. However, when trying to calculate I get mixed results, none of them being the correct total. The results range from nil, 0.0 and -2.0 depending on whether I have .to_f included.
The first calculation is based on a percentage discount and the second on a straight money reduction.
I have performed the calculations in the view (to test) and they display correctly, but when they are moved over to the controller, I get the errors.
Any help is much appreciated.
since u r converting the values to float, ur calculation results arent precise.
see String, decimal, or float datatype for price field?

SQL math syntax difficulty with division

Can't understand why I can't get the correct answer. I'm trying to calculate a net margin percentage but the divide portion is being ignored. Hopefully really simple one?
SUM(
(dbo.K3_TradeTeam_Sales2.TotalSales - dbo.K3_TradeTeam_SalesReturn3.TotalCredits)
ISNULL(dbo.K3_TradeTeam_Purch1.TotalPurchases, 0) /
dbo.K3_TradeTeam_Sales2.TotalSales
) AS NetMargin
To get a net margin you probably want an expression like this,
(
SUM(
dbo.K3_TradeTeam_Sales2.TotalSales
-
COALESCE(dbo.K3_TradeTeam_SalesReturn3.TotalCredits, 0.0)
-
COALESCE(dbo.K3_TradeTeam_Purch1.TotalPurchases, 0.0)
)
/
SUM(
dbo.K3_TradeTeam_Sales2.TotalSales
)
) AS NetMargin
However, without knowing your schema I couldn't say for sure. This would also fail miserable when you have 0.0 total sales.
I'm assuming that your want the sum of net profits for each realtion divided by the sum of the total revenue for each relation. This would give you the cumulative profit margin for all relationships.
The sql logic should work.
Are you sure that TotalPurchases is not null? Because this makes it look like the devide is ignored, but the devide just returns 0. So I'm doing minus zero!
examples:
select SUM((200 - 100) - ISNULL(100, 0) / 10) AS NetMargin --returns 90
select SUM((200 - 100) - ISNULL(null, 0) / 10) AS NetMargin --return 100
When I am having trouble getting a math formula to work, I change the select temporarily to simply see the values of each of the fields as well as the calculation. Then I can do the calualtion manually to see how I am getting the results I am getting which alwmost always points me to the problem.
select
dbo.K3_TradeTeam_Sales2.TotalSales,
dbo.K3_TradeTeam_SalesReturn3.TotalCredits,
ISNULL(dbo.K3_TradeTeam_Purch1.TotalPurchases, 0),
dbo.K3_TradeTeam_Sales2.TotalSales
From...
I also never do a division without a case statement to handle the divisor being 0 problem. Even if I think it can never happen, I have seen it burn people too often not to consider that something (including bad data entry) might make it happen in the future.

Using T-SQL how do you calculate a test grade percentage?

It what should be a simple task, or a simple google search, I have come up empty. I am trying to simply calculate a grade percentage. I am trying to use:
Select round((133 / 150), 2) * 100
That results in 0. I am trying to calcualte to get a score of 89%. I have tried multiple combinations and I am beginning to think it is either too complicated to do with t-sql or it is not possible. What am I missing?
Try this:
Select round((cast(133 as numeric(20,8))/ cast(150 as numeric(20,8))) * 100, 2)
You are using integers in your example, when you need decimal values. See here for more information.
Try:
Select round(133.0 / 150) * 100
An int divided by an int is an int, and 1 / 2 = 0 (integer truncation)
More generally, if you ensure numerator is a float (real/decimal):
Select round(CAST(integervalue as real) / 150) * 100

SQL query to calculate coordinate proximity

I'm using this formula to calculate the distance between entries in my (My)SQL database which have latitude and longitude fields in decimal format:
6371 * ACOS(SIN(RADIANS( %lat1% )) * SIN(RADIANS( %lat2% )) +
COS(RADIANS( %lat1% )) * COS(RADIANS( %lat2% )) * COS(RADIANS( %lon2% ) -
RADIANS( %lon1% )))
Substituting %lat1% and %lat2% appropriately it can be used in the WHERE clause to find entries within a certain radius of another entry, using it in the ORDER BY clause together with LIMIT will find the nearest x entries etc.
I'm writing this mostly as a note for myself, but improvements are always welcome. :)
Note: As mentioned by Valerion below, this calculates in kilometers. Substitute 6371 by an appropriate alternative number to use meters, miles etc.
For databases (such as SQLite) that don't support trigonometric functions you can use the Pythagorean theorem.
This is a faster method, even if your database does support trigonometric functions, with the following caveats:
you need to store coords in x,y grid instead of (or as well as) lat,lng;
the calculation assumes 'flat earth', but this is fine for relatively local searches.
Here's an example from a Rails project I'm working on (the important bit is the SQL in the middle):
class User < ActiveRecord::Base
...
# has integer x & y coordinates
...
# Returns array of {:user => <User>, :distance => <distance>}, sorted by distance (in metres).
# Distance is rounded to nearest integer.
# point is a Geo::LatLng.
# radius is in metres.
# limit specifies the maximum number of records to return (default 100).
def self.find_within_radius(point, radius, limit = 100)
sql = <<-SQL
select id, lat, lng, (#{point.x} - x) * (#{point.x} - x) + (#{point.y} - y) * (#{point.y} - y) d
from users where #{(radius ** 2)} >= d
order by d limit #{limit}
SQL
users = User.find_by_sql(sql)
users.each {|user| user.d = Math.sqrt(user.d.to_f).round}
return users
end
Am i right in thinking this is the Haversine formula?
I use the exact same method on a vehicle-tracking application and have done for years. It works perfectly well. A quick check of some old code shows that I multiply the result by 6378137 which if memory serves converts to meters, but I haven't touched it for a very long time.
I believe SQL 2008 has a new spatial datatype that I imagine allows these kinds of comparisons without knowing this formula, and also allows spatial indexes which might be interesting, but I've not looked into it.
I have been using this, forget where I got it though.
SELECT n, SQRT(POW((69.1 * (n.field_geofield_lat - :lat)) , 2 ) + POW((53 * (n.field_geofield_lon - :lon)), 2)) AS distance FROM field_revision_field_geofield n ORDER BY distance ASC