473,395 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calculation across Tables

Hi all, I am facing difficulties on creating a particular query.

Sample of my data table 1: (DetailsTable)

Order | Material | Quantity | Weight
123 | Orange | 10 |
123 | Apple | 20 |
123 | Guava | 20 |
233 | Orange | 10 |

Sample of my data table 2:(WeightsTable)

Order | Weight |
123 | 100 |
233 | 20 |

How can I create query to update my DetailsTable and allocate the weights proportionately (by quantity) to the individual order's material.

Sample outcome of data table1: (DetailsTable)

Order | Material | Quantity | Weight
123 | Orange | 10 | 20
123 | Apple | 20 | 40
123 | Guava | 20 | 40
233 | Orange | 10 | 20

Where weight of Order 123's Orange = 10/(10+20+20) * 100.

Thank you so much in advance.

Joe
Sep 29 '11 #1

✓ answered by Rabbit

If you wanted to store the weights of the individual line items in the first place, why have a weights table at all?

Since this is a calculation, it's better not to store the results of the calculation but to calculate on an as needed basis.

To calculate the individual weights, you can join the two tables and use a subquery to get the sum of the order quantity.

3 1912
Rabbit
12,516 Expert Mod 8TB
If you wanted to store the weights of the individual line items in the first place, why have a weights table at all?

Since this is a calculation, it's better not to store the results of the calculation but to calculate on an as needed basis.

To calculate the individual weights, you can join the two tables and use a subquery to get the sum of the order quantity.
Sep 29 '11 #2
Hi Rabbit,

The reason that the weights table exist is that the items are measured in orders and not individually.

However, during analysis, I would need the weights of individual items hence I have assumed that the weights is proportional to the quantity.

I was wondering if there is a simple query that can assist me in filling up the weights such as:
Expand|Select|Wrap|Line Numbers
  1. Update DetailsTable INNER JOIN WeightsTable on DetailsTable.Order= WeightsTable.Order
  2. Set DetailsTable.Weight = WeightsTable.Weight* DetailsTable.Quantity/(SUM(DetailsTable.Quantity)
  3. Group by Order
However, this query gives an error message.

Thank you so much in advance.
Oct 4 '11 #3
Rabbit
12,516 Expert Mod 8TB
You'll want to try the subquery instead of the inner join.
Oct 4 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jason | last post by:
I realize that FK cannot be created across databases. The question I have is what are the alternatives? In my thinking I have two choices. 1) First choice (which I read in a different post) is...
3
by: jimstallings | last post by:
Hi, I am trying to locate documentation on using views across databases. We have serveral databases broken down by application, some shared databases and some location specific databases...
1
by: Rick Brandt | last post by:
I know that a heterogeneous query joining tables from two different servers has performance penalties but is the same true when joining tables from two different databases on the same SQL Server...
0
by: OhMyGaw | last post by:
Hey guys, Couldn't find this anywhere in google. I want a list of all database column names for a specific table/view from across database. I tried this......
1
by: chandrub78 | last post by:
Hello Everyone We have a third party tool which maintains its own custom database,DB1 on SQL Server 2000 on Server A (Windows 2000). We built an inhouse application which maintains a transactional...
6
by: Damon Grieves | last post by:
Hi I just want to be sure I understand how the Access client works. If I have an Access back end with a million records on a server and an Access client. If the client is installed on the users pc...
1
by: WayneW | last post by:
I need to create a view of tables in 2 databases. Is this possible?
0
by: pskaar | last post by:
Hello, This is my first time but hopefully I can get an answer. I ran a bad SQL in production that updated data erroneously. What I'm hoping to be able to do is this: After a few tables...
1
by: astrix | last post by:
Hi, I would like to know if it is possible to do an exchange partition between a partitioned table on dbA and a non partitioned table on dbB using db links. say I have a partitioned table -...
1
by: rajavale | last post by:
Hello, Can someone tell me how one would join tables across different databases, the databases being in the same instance. select <field> from database1.table1 a, database2.table1 b where...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.