473,289 Members | 1,945 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,289 software developers and data experts.

How to retrive data from four tables

samikhan83
i am having a problem in getting data from four tables based on a common field in one query.

1. i have product,open_balance,purchase,sales tables and product_id is primary key of product and foreign key in rest of the tables
2. i want to get the sum of the open_balance,purchase and sales for a product based on product key to get current quantity in stock

i used Left Join i think i am not writing the query correctly because it is doubling the value by repeating data based on conditions in query
This is the query

Expand|Select|Wrap|Line Numbers
  1. select 
  2.   p.p_manufacturer,
  3.   p.p_name,
  4.   sum(oqp.op_q_p_quant)as open_balance,
  5.   sum(pqp.pur_q_p_quant) as purchase_balance,
  6.   sum(sqp.sale_q_p_quant) as sales_balance 
  7. from product p 
  8. Left Join purchase_quant_price pqp 
  9.   on p.p_id=pqp.pur_q_p_pro_id 
  10. Left Join open_quant_price oqp 
  11.   on p.p_id=oqp.op_q_p_pro_id 
  12. left join sales_quant_price sqp 
  13.   on p.p_id=sqp.sale_q_p_pro_id;
  14.  
Please help me
Thanx in advance
Dec 4 '08 #1
3 2194
code green
1,726 Expert 1GB
GROUP BY the common field should solve it.
Or any unique field.
T-SQL for example would throw an error for this query because if one field is aggegated they all need to be.
This actually would have helped you in this situation.
Dec 5 '08 #2
hi code green,
thanks for your reply.
yes i too thought that but according to my knowledge of database i am not getting to the solution

before i use to do this with getting individual product_id by looping over product table and passing product_id to other tables and getting required data from that tables based on that product id

but i think this is very in-efficient solution and required too much looping and code and i think getting solution in this way is not a good practice.

so, please help me with this
Dec 5 '08 #3
code green
1,726 Expert 1GB
Try
GROUP BY p.p_id
This would only return unique p_id.
If that is what you are looking for
Dec 8 '08 #4

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

Similar topics

10
by: HB Kim | last post by:
Hello, What could possibly cause data in the SQL server database to be removed, except being deleted manually? We had a couple of situations where data in certain records disappeared although the...
2
by: serge | last post by:
My project is to automate testing of Stored Procedures of type SELECT (at least for now). I want to create a table where each stored procedure's input parameter values are entered and in another...
17
by: Wilfried | last post by:
Hi, I have 5 tables: main data1
1
by: john woo | last post by:
Hi I'm not sure it's possible that using javascript to locate/get components X|Y then other language, like java-jsp to save/retrive these data. For instance, a GUI presents a layout of...
3
by: hpaisawala | last post by:
Hiya, Is it possible to create a command button on a form and code it such that it goes to a particular field and changes the data type property of that filed from Auto Number to Number. Many...
1
by: NumberCruncher | last post by:
Hi All, I am struggling with setting up my first system of tables, forms,and reports, and could use your help! I am setting up a database to keep track of the production of a produced item. The...
1
by: jpr | last post by:
Hello, My database has 5 tables. WHen I add data to one table, it runs an append query that copies three records to other 4 tables. The main table is MASTER. The data I copy are: ID, SSN and...
23
nehashri
by: nehashri | last post by:
hi i am designing a database using Ms Access and ASP. i have 3 tables in access namely 'PERSONAL', other as 'POLICY' and 3rd one is named as 'STAFF'. in the contact table i have ID, Name, Children...
5
Inbaraj
by: Inbaraj | last post by:
Hi.. In oracle i am having 100 tables each contains more then 500 records... Due to virus attack i lost the C drive (OS) in my PC but i installed oracle in D: drive. No i want to retrive the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.