Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 2nd, 2008, 06:42 PM
Lokean's Avatar
Member
 
Join Date: Apr 2007
Posts: 71
Default SQL newbie, need help cleaning up my SQL (works, but ugly)

Okay, this works, but there has GOT to be a cleaner way of doing this.

Suggestions please.

[HTML]DECLARE @TMP TABLE
(
HOLD1 NUMERIC,
HOLD2 NUMERIC,
HOLD3 NUMERIC,
HOLD4 NUMERIC,
HOLD5 NUMERIC
)
INSERT INTO @TMP (HOLD1, HOLD2)
select PARENT_MEMBER_ID,null
from mydb.RELATIONSHIP
where child_member_id =2824

UPDATE @TMP
SET HOLD2 = PARENT_MEMBER_ID
from mydb.RELATIONSHIP
where child_member_id = HOLD1

UPDATE @TMP
SET HOLD3 = PARENT_MEMBER_ID
from mydb.RELATIONSHIP
where child_member_id = HOLD2

UPDATE @TMP
SET HOLD4 = PARENT_MEMBER_ID
from mydb.RELATIONSHIP
where child_member_id = HOLD3
UPDATE @TMP

SET HOLD5 = PARENT_MEMBER_ID
from mydb.RELATIONSHIP
where child_member_id = HOLD4
select * from @tmp
select
gp4.GROUP_NAME,
gp3.GROUP_NAME,
gp2.GROUP_NAME ,
gp1.GROUP_NAME
from @TMP as tmp
left outer JOIN mydb.GROUP_PROFILE as gp1
on tmp.Hold1 = gp1.MEMBER_ID
left outer JOIN mydb.GROUP_PROFILE as gp2
on tmp.Hold2 = gp2.MEMBER_ID
left outer JOIN mydb.GROUP_PROFILE as gp3
on tmp.Hold3 = gp3.MEMBER_ID
left outer JOIN mydb.GROUP_PROFILE as gp4
on tmp.Hold4 = gp4.MEMBER_ID
[/HTML]
Reply
  #2  
Old October 3rd, 2008, 12:11 AM
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 591
Default

Some explanatory comments would help.
We could read through the code and try and decipher and or guess what you are doing but that would take unnecessary time. So, explanatory comments please!

Regards
Reply
  #3  
Old October 3rd, 2008, 05:35 PM
Lokean's Avatar
Member
 
Join Date: Apr 2007
Posts: 71
Default

Quote:
Originally Posted by Delerna
Some explanatory comments would help.
We could read through the code and try and decipher and or guess what you are doing but that would take unnecessary time. So, explanatory comments please!

Regards

Sorry.

I am navigating through a tree structure.

Each "hold" item is a different level on the tree.

I start by feeding the query the serial number of the product I am looking for, then query for all of the parent groups that have this product in it. The product can have multiple parents.

I then go up another level to get the parents of those groups which each only have a single parent.

What I am ultimately trying to do is trace the product to each of it's root level ancestors and tracking all of the ancestors along the way.

Example:

If I have product 2227, it's immediate parents may be:

ABC_account
ABC_sales_account
XZY_Products

Then the final results would be like this.

Customer_accounts/USA_accounts/ABC_account/2227
Sales_accoucts/USA_Sales/ABC_sales_account/2227
Product_portfolio/USA_Products/XYZ_products/22y
Reply
Reply

Bookmarks

Thread Tools

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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles