Solved

Complex arithmetic calculations in SQL

Posted on 2016-08-24
3
53 Views
Last Modified: 2016-08-25
Hi everyone,

I have a situation where I have to get the result in SQL of somehow complex calculation which we were previously doing it in Ms Excel. Attached is the file to see the table structure and data. Now by keeping in mind the structure of the table, kindly help how to solve the following formula for "UK" where "LocationID" is different but "LocationSubID" are same.

Average = (((Itemsold_month-Itemsold_day)*Itemsold_total)+((Itemsold_month-Itemsold_day)*Itemsold_total))/(Itemsold_month-Itemsold_day)

This formula will be used to calculate for both entries of "UK" but their will be a single value, say "Average"

Please guide. If need any further clarification then please let me know.

P.S. This formula can be simplified but need to calculate the result using above mentioned formula only

Thanks.
Sample-table.xlsx
0
Comment
Question by:hennanra3
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Randy Peterson earned 500 total points
ID: 41768951
So what happens when you have 3 different locations in the same country?  What you are trying to do is to get the average ((Itemsold_month-Itemsold_day)*Itemsold_total) for each country correct?  

Your formula is getting (from your table) Average = (((Itemsold_month-Itemsold_day)*Itemsold_total)+((Itemsold_month-Itemsold_day)*Itemsold_total))/(Itemsold_month-Itemsold_day)

But you are getting different values in your formula from different rows in your table?  Just trying to get the exact way you are trying to calculate.  SQL is very powerful doing aggregations and formulas like this, I just want to get it right.
0
 
LVL 8

Expert Comment

by:Randy Peterson
ID: 41768955
Basically, if you could provide one concrete example from your table of your calculation, I should be able to generate the query.
0
 

Author Closing Comment

by:hennanra3
ID: 41769847
Yes agree SQL is indeed very powerful in doing aggregations. Used "SUM" and their result in query to get the result.
Thanks.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now