here is the code
Main Topics
Browse All TopicsWe have MS SQL 2000. We have a table of Items where we store Parent Items and Child Items. The Parent Items are the way we purchase (in Cases) and the Child Item is the way we sell (in Each).
We've been working in a script where we can display the Parent Item, but with its child calculated item Cost. The thing is that both items are in the same table.
Here's the Table contents:
ID ItemCode ItemParentId Cost Packaging
-- ----------------- --------------- -------- ---------------
51 Parent01 0 24.00 0
50 Child01 51 1.00 24
55 RegularItem1 0 15.95 0
59 RegularItem2 0 7.50 0
66 Parent02 0 65.90 0
75 Child02 66 1.83 36
The child item has a the ID of the parent is related to (column "ItemParentId"); for example, Item Child02 is the Child of Parent02 based on the ID/ItemParentId.
We need a script that will display the following:
Parent Items ParentCost ChildCost
---------------- ---------------- ----------------
Parent01 24.00 1.00
Parent02 65.90 1.83
Hope this was clear
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
DECLARE @ItemTable TABLE(ID INT,ItemCode VARCHAR(255),ItemParentId INT,Cost DECIMAL, Packaging INT)
-- ID ItemCode ItemParentId Cost Packaging
-- -- ----------------- --------------- -------- ---------------
INSERT INTO @ItemTable VALUES ( 51,'Parent01', 0, 24.00, 0 )
INSERT INTO @ItemTable VALUES ( 50,'Child01', 51, 1.00, 24 )
INSERT INTO @ItemTable VALUES ( 55,'RegularItem1', 0, 15.95, 0 )
INSERT INTO @ItemTable VALUES ( 59,'RegularItem2', 0, 7.50, 0 )
INSERT INTO @ItemTable VALUES ( 66,'Parent02', 0, 65.90, 0 )
INSERT INTO @ItemTable VALUES ( 75,'Child02', 66.00, 1.830, 36 )
SELECT * FROM @ItemTable
SELECT ParentItemTable.ItemCode,
SUM(ParentItemTable.Cost),
SUM(ChildItemTable.Cost)
FROM @ItemTable AS ParentItemTable
INNER JOIN @ItemTable AS ChildItemTable ON ParentItemTable.ID = ChildItemTable.ItemParentI
GROUP BY ParentItemTable.ItemCode
Hi MelissaSuciadi, I tried your third script and it didn't work, it return empy. But when I deleted the last line ("where p.ParentItem is null"), it seems to work. Can you validate your script :
select p.itemcode , p.cost as parentCost, p.cost/c.packaging AS ChildCost
from tableName p join
(select * from tableName where parentid is not null) as c
ON p.id = c.parentId
-- where p.parentid is null (deleted, it seems to work)
NOTE: but when I tried your last script, it does work with the last line. Let me say that the column parentid is never null (just in case), Iwould this change anything logic in your script?
ImranTaj, I dont have 2 tables, both Parent and Child are in the same table.
ranned it and it return empty.
Listen, but your your previous script (see below) works fine. Can I replace the NULL for the 0?
select p.itemcode , p.cost as parentCost, p.cost/c.packaging AS ChildCost
from tableName p join
(select * from tableName where parentid is not null) as c
ON p.id = c.parentId
-- where p.parentid is null (deleted, it seems to work)
Business Accounts
Answer for Membership
by: RamantePosted on 2009-10-01 at 01:47:37ID: 25466871
To finish the example of Item Child02, the Child02 is related to Parent02 because the ItemParentId=66 which is the Parent02 ID of 66. ItemParentId is the column that tells the Child Item which Parent Item is related.