Skip to content
Advertisement

Duplicate records in table after left join

I have three tables.

“Departments” table: department_name, department_id.
Departments table

Table “LowPrice”: departmend_id, product_name, purchased, sold, price.
LowPrice table

Table “Basic”: departmend_id, product_name, purchased, sold, price.
Basic table

In the tables “Basic” and “Low” there can be several records pertaining to one department.

There is no relations between these tables. I want to develop a query that will group records from the Low and Basic tables by the department number from the Departments table. When grouping, the cost of sold and purchased goods from the “Basic” and “Low price” tables for each department is summed up.

My query code:

But now only those departments for which there are records in only one table are counted correctly. If there are records for a department in two tables, then their number is multiplied and the query gives an incorrect result.

Result I have

Expected result

Please enlighten me what I need to change in my query.

Advertisement

Answer

Consider combining LowPrice and BasicPrice into one table since qualitatively they contain the same information. Simply, add a Type field to designate low price and basic price records.

Make-Table Query (to be run once)

Once your properly normalized those two relations for more efficient storage, run conditional aggregation. Below uses table aliases and calculated columns which is supported in MS Access.

Conditional Aggregation Query

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement