473,802 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to retrive data from four tables

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

1. i have product,open_ba lance,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,pu rchase 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 2221
code green
1,726 Recognized Expert Top Contributor
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
samikhan83
33 New Member
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 Recognized Expert Top Contributor
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
6372
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 records were still there. The data is entered and editted through the web interface in ASP. The web interface is accessed by anyone who has an account in our database. I am more of a web programmer, not a SQL server administrator, so not very...
2
4266
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 table the expected result value(s) are entered when executed against a sample database containing manually entered and verified data. My current problem is that the stored procedures' input
17
2079
by: Wilfried | last post by:
Hi, I have 5 tables: main data1
1
1370
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 tables/chairs for a exhibition, some small images represent tables/chairs of various sizes/styles, which considered as movable components in this GUI, and their positions specified by X|Y.
3
1765
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 Thanks and Kind Regards, Hussain
1
4787
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 item is a panel, with a specific texture. There are standard panels that are then produced in a limited number of specific textures. The number of panels/project varies, so I keep track of the actual number of panels per project with the count:...
1
4499
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 PURGE DATE. I would like to create a delete query that would delete all records in all tables that show a date older that the one in the PURGE DATE. I use
23
3426
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 as fields. Also in policy table the firlds are:- ID, date_of_policy, no_policy, amount_paid, amount_balance and similarly the 3rd ie., staff has few fields. all three are linked my a common field which is ID all i want is when the user searches...
5
1863
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 data can any one help me how to do this... its urgent for me... thanks in advance.......... with reg Inba
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10304
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.