I've got stuck on some mathematical action that I perform in SQL Server 2016 Enterprise.
I need to calculate this expression:
(4.384 / 4.2989 * 100) * 98.8251017928029 / 100
In SQL I get the result 100.78141988869389772850690000
But when I calculate this expression in MS Excel, I get: 100.7814199585120000
Since these results is a Consumer Price index, the numbers after the decimal point do matter.
So, my question is, which result is correct? SQL Server or Excel.
PS. I have updated my question.
Here is dbfiddle
Thank you.
I found same problem here: Wrong calculation in SQL-Server
I've also tried the calculation with java, and it shows the result of the excel is more accurate.
You should use decimal in calculation using sql.
Actualy, creating DbFiddle I found errors in cast. So when errors was fixed the result now the are almost the same as in excel. There is small difference in results but the difference is behind my needs of scale
Related
I have this query that is clearly producing duplicates, but I don't see why since I have the DISTINCT option in use here.
I just migrated SQL servers from one running SQL version 12.0.6329.1 to 13.0.6419.1 (2014 to 2016 I believe)
and I don't experience the same issue on the old server.
Any ideas why DISTINCT isn't working as [I] expected?
SELECT DISTINCT
[UWI_vn]
,[WI_PrdWellCnt]
,[AAV_GUID]
,[InResFlag]
FROM [AAV_WellStore].[dbo].[V_ResultsProdBdgtOpsUpLiveBaseV4.5]
WHERE [InResFlag] =1
AND [WI_PrdWellCnt] > 0
AND [UWI_vn] = '102/16-25-069-05W6/0'
Thanks to dfundako for the checksum trick and ThorstenKettner and KeithL for bringing up the intrinsic properties of the float column.
The [WI_PrdWellCnt] column is a float and goes through a CTE that aggregates hundereds of rows to get down to one row. This average must be whats causing the issue. You'd expect the average to be the same if all values are the same, but they arent We've broken this value out and calculated it separately so we don't have to deal with this issue.
Casting [WI_PrdWellCnt] to Real would also potentially solve the issue. (Casting to Decimal as suggested in this thread loses precision and rounds up to 1 in my example) The tables are produced by a proprietary application so altering the base tables is not an option.
I've come across a bit of code that is used to validate a number inputted.
It uses a percentage sign but is nothing to do with any LIKE or varchar functions - it is doing some sort of calculation but I cannot figure it out.
Essentially it looks like this: 1 % 11
If the second number is bigger than the first it will always bring back the first, but if the second is less than the first it brings back strange results.
Does anyone know what this function is doing?
It is modulo operator (division remainder). See MSDN for details.
Initially there were 1000 records to evaluate.Now there are 40000.Please help!
I'm only trying to obtain the week number of a transaction based on transaction date and start date.
SELECT [1_Webtime_By_Date].Badge,Int(((([1_Webtime_By_Date].Date-Forms![Date Form].StartDate)+1.99)/7)+1) AS Week
FROM 1_Webtime_By_Date
GROUP BY [1_Webtime_By_Date].Badge,Int(((([1_Webtime_By_Date].Date-Forms![Date Form].StartDate)+1.99)/7)+1);
This is a known issue with the compiler used by Access. Limits of 64K segments were lifted after Access 97 but the amount of data you are quering is simply too much for Access. There are a few tips given on the following page that may help but it seems to me that you need to use a proper database system such as MS SQL. There is a free version available (SQL Express) if it's cost that is the problem.
ACC: "Out of Memory" or "Query Too Complex" with Query/Report
SQL Server Express Download page
You may find that using SQL Server as a database and Access as a front end may help your problems if you are tied to using Access for end users.
The best tip given is to use aliasing to shorten the length of your query and to try to remove nested queries:
Access SQL: FROM clause
My first inclination is to add some white space before and after both the minus signs.
My next test would to make the form field into a DATE type, and use something like
DATEPART(ww,[1_Webtime_By_Date].Date - Forms![Date Form].StartDate ) AS WK
If I wanted to run a query where certain fields in my query were to be mispelt (up to a certain point of course), is there a native way to do this with MS SQL server or does one have to implement something else entirely?
What I think it boils to is can you select where levenstein distance is a max of X
Example
select from myTable WHERE name closeTo('Erik')
and it might return things like Eric, ie, a Levenhstein distance of 1
Have a look at T-SQL SOUNDEX and DIFFERENCE: http://msdn.microsoft.com/en-us/library/ms187384.aspx
I'm a novice when it comes to SQL and PHP, and I'm trying to round an average price result and take off the extra zeroes that currently appear.
Currently my result turns up as: $3.005000
My code currently reads as follows:
$result = mysql_query("SELECT AVG(price) FROM milk WHERE price > 0 ");
$row = mysql_fetch_row ($result);
I have found several examples of SQL rounding and truncation but unfortunately the tutorials I've seen provide me with no useful information on where or how I am supposed to implement these changes.
This leaves me making guesses on where to make changes -- none of which have worked out so far (obviously).
If someone could provide me with an example of how to round and truncate my results, which includes where exactly I need to make these changes in my current configuration, that would be most helpful and I would be very thankful! I'm really sorry if my n00bishness makes it more difficult to explain the solution.
Thanks!
Formatting of the data should be done in the script making the query, not in the query itself. For example, in PHP you can write the following using sprintf:
$formatted_price = sprintf("%01.2f", $unformatted_price);
(Example complements of the PHP manual).
Also, generally, price values are stored as decimal types or scaled integers, not floating-point types, since floating-point values are not exact.
MySQL has a ROUND() function.
So just round your average in your SQL query:
$result = mysql_query("SELECT ROUND(AVG(price),2) FROM milk WHERE price > 0 ");
If you end up with formatting issues, you can use PHP's number_format() function during output.