Solved

Complex arithmetic calculations in SQL

Posted on 2016-08-24
3
122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

717 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