If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > help on query..

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-19-06, 06:59
ronysamuel ronysamuel is offline
Registered User
 
Join Date: Dec 2006
Posts: 8
help on query..

i got an assignment on sql.. but i cant complete the query.. i'm working on it from yesterday.. pls help me..

Create tables with corresponding fileds as described below

itermaster
itemcode
name
price

party master
partycode(PK)
name

purchase master
date
partycode(fk)
PO Number


purchase details
PO Number
itemcode(FK)
qty


sales master
date
partycode(FK)
Sales Number


Sales details
Sales Number
item code(FK)
qty



Generate reports using SQL which contains
name,price,Tot. Purchased,Total Sales,Balance
and

Name,Item,Purchase,Sales


Some example datas are given below for reference.




item master

itemcode(pk) name price
1 Lux 12
2 Cinthol 16
3 Colgate 18
4 Ponds 10
5 Siscor 20




party master
partycode(PK) name
1 Jaya Agencies
2 Buraaq
3 Mascot
4 N R Trading Co.
5 Meridian
6 V.S Agencies
7 RMPL
8 VV
9 ChaPuPu



purchase master
date partycode(fk) PO Number
01/07/06 4 P001
05/07/06 2 P002
06/07/06 3 P003


purchase details
PO Number itemcode(FK) qty
P001 1 5
P001 2 10
P001 4 7
P001 5 10
P002 4 15
P003 1 10



sales master
date partycode(FK) Sales Number
07/07/06 6 s001
08/07/06 8 s002

Sales details
Sales Number item code(FK) qty
s001 2 1
s002 4 4
s001 2 2
s002 1 3
s002 1 1



Generate reports using SQL which contains
name,price,Tot. Purchased,Total Sales,Balance
and

Name,Item,Purchase,Sales

Query 1.

code name price Tot. Purchased Total Sales Balance
1 Lux 12 10 4 6
2 Cinthol 16
3 Colgate 18
4 Ponds 10
5 Siscor 20 10 0 10



Query 2.
Code Name Item Purchase Sales
1 Jaya Agencies
2 Buraaq Cinthol 40 0
Colgate
Ponds
3 Mascot
4 N R Trading Co.
5 Anees
6 Neeethu
7 Anitha
8 George
9 Ranjusha

thanks in advance..
Reply With Quote
  #2 (permalink)  
Old 12-19-06, 07:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sorry, we will not do homework assignments
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-19-06, 07:16
ronysamuel ronysamuel is offline
Registered User
 
Join Date: Dec 2006
Posts: 8
i want to know how it can be done.. not to copy and present the assignment.. pls help me..
Reply With Quote
  #4 (permalink)  
Old 12-19-06, 07:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
how can what be done? what is your question?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 12-19-06, 07:24
ronysamuel ronysamuel is offline
Registered User
 
Join Date: Dec 2006
Posts: 8
its given in my post.. it is actuallly to join three tables which adds values from two tables and display all the records even if it is null.. using left join we can do this..

i wrote this query:

select im.itemcode, im.name, im.price, sum(pd.qty), sum(sd.qty) from itemmaster im
left join
purchasedetails pd on im.itemcode = pd.itemcode
left join
salesdetails sd on im.itemcode = sd.itemcode
group by im.itemcode, im.name, im.price;

but the sum value is getting doubled..
Reply With Quote
  #6 (permalink)  
Old 12-19-06, 08:39
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Imagine that for every itemmaster record there exists two purchasedetails records and two salesdetails records. When the table are joined in the manner you have used, this results in four possible combinations of the purchase and sales records. Each purchase record appears twice in the resultset, as does each sales records, and so when you aggregate the results you end up doubling the summary value.

One solution is to use a nested subquery to calculate purchase totals and sales totals independently:

Code:
select	itemcode,
	sum(qty) as purchaseqty
from	purchasedetails
group by itemcode
gives you the total purchase quantity for each item. Now write the same query for salesdetails and look up the syntax for joining these to the itemmaster table as subqueries.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 12-20-06, 02:01
ronysamuel ronysamuel is offline
Registered User
 
Join Date: Dec 2006
Posts: 8
i got the query..

select im.itemcode,
im.name,
im.price,
purchaseqty,
salesqty,
(purchaseqty-salesqty) as balance
from itemmaster im
left join
(select purch.itemcode as code,
purchaseqty,
salesqty
from (select itemcode, sum(qty) as purchaseqty from purchasedetails group by itemcode) as purch
left join
(select itemcode, sum(qty) as salesqty from salesdetails group by itemcode) as sales
on purch.itemcode=sales.itemcode) as foo
on im.itemcode=foo.code

but its not displaying the difference in balance column if salesqty is null.. any idea how to do..?

thanks for all the help u've given..

and can we do this same query eliminating subqueries and making it a simple query..?
Reply With Quote
  #8 (permalink)  
Old 12-20-06, 09:09
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Ok, you're trying, so I'll keep helping you.

Scrap the code you just did. It was a noble effort, but you need to change your way of thinking.

Write three querys for me:
1) A query to return just the ItemMaster data
2) A query to return the quantity purchased for each itemcode (Hey! I already did this one for you! )
3) A query to return the quantity sold for each itemcode

Post these three statements to the forum.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #9 (permalink)  
Old 12-20-06, 12:57
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
What platform are you coding, and what school and grade are you in?
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #10 (permalink)  
Old 12-20-06, 22:39
ronysamuel ronysamuel is offline
Registered User
 
Join Date: Dec 2006
Posts: 8
i'm working on postgresql 1.4..
i'm studying BSc Comp Sci.. 2nd year..
Reply With Quote
  #11 (permalink)  
Old 12-20-06, 23:06
ronysamuel ronysamuel is offline
Registered User
 
Join Date: Dec 2006
Posts: 8
dear blindman,

here's the queries u asked..

1) select name,
itemcode
from itemmaster;

2) select itemcode,
sum(qty) as purchaseqty
from purchasedetails
group by itemcode;

3) select itemcode,
sum(qty) as salesqty
from salesdetails
group by itemcode;
Reply With Quote
  #12 (permalink)  
Old 12-21-06, 11:37
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Great. Now you assemble them like Lego Blocks:

Code:
select	itemmaster.name,
	itemmaster.itemcode,
	PurchaseTotals.purchaseqty,
	SalesTotals.salesqty
from	itemmaster
	left outer join --PurchaseTotals
		(select	itemcode,
			sum(qty) as purchaseqty
		from	purchasedetails
		group by itemcode) PurchaseTotals
		on itemmaster.itemcode = PurchaseTotals.itemcode
	left outer join --SalesTotals
		(select	itemcode,
			sum(qty) as salesqty
		from	salesdetails
		group by itemcode) SalesTotals
		on itemmaster.itemcode = SalesTotals.itemcode
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #13 (permalink)  
Old 12-27-06, 10:35
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Gee, glad to have done your homework for you.

Gosh, you are SO welcome!
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #14 (permalink)  
Old 12-29-06, 15:26
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Do I need to buy a new spectacles, or is a post #12 3/4 really missing?

I just hope that ronysamuel didn't die of happiness; much better scenario is that he simply forgot to say "thank you!".
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On