SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Self Joins in SQL Server 2005


Self Joins in SQL Server 2005

Author
Message
Boby B Jacob
Boby B Jacob
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 77
Is there any better way of implementing self joins in SQL Server.? I mean, instead of looping the same table many times, can we achieve it using some other method? Please help
Gianluca Sartori
Gianluca Sartori
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24369 Visits: 13362
A bit vague, can you clarify please?
Do you have an example?

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Boby B Jacob
Boby B Jacob
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 77
This is the query... Can we achieve the same via some other method?

SELECT OS.NAME, OS.ITEMID, OS.PARENTID, OS2.NAME AS PARENTNAME, OS.HIERID, OS3.NAME AS HIERNAME,OS.HIERLEVEL,H.LEVELNAME
FROM OPERATIONALSTRUCTURE OS, OPERATIONALSTRUCTURE OS2, OPERATIONALSTRUCTURE OS3,HIERLEVELS H
WHERE OS.PARENTID = OS2.ITEMID AND OS.HIERID = OS3.ITEMID AND H.HIERID=OS3.HIERID AND H.HIERLEVEL=OS.HIERLEVEL
ORDER BY OS.HIERID, OS.PARENTID, OS.NAME
Gianluca Sartori
Gianluca Sartori
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24369 Visits: 13362
You can achieve the same thing with subqueries. If you're after better performance, I'm not sure that rewriting the query this way will improve things.
Also, consider that the semantics changed slightly, as now you're doing OUTER joins instead of INNER joins. Make sure this is ok in your case.


SELECT OS.NAME
,OS.ITEMID
,OS.PARENTID
,PARENTNAME = (
SELECT NAME
FROM OPERATIONALSTRUCTURE AS OS2
WHERE OS.PARENTID = OS2.ITEMID
)
,OS.HIERID
,HIERNAME = (
SELECT NAME
FROM OPERATIONALSTRUCTURE AS OS3
WHERE OS.HIERID = OS3.ITEMID
AND H.HIERID = OS3.HIERID
)
,OS.HIERLEVEL
,H.LEVELNAME
FROM OPERATIONALSTRUCTURE AS OS
INNER JOIN HIERLEVELS H
ON H.HIERLEVEL = OS.HIERLEVEL
ORDER BY OS.HIERID
,OS.PARENTID
,OS.NAME



Another possibility is CROSS APPLY:

SELECT OS.NAME
,OS.ITEMID
,OS.PARENTID
,PARENTNAME
,OS.HIERID
,HIERNAME
,OS.HIERLEVEL
,H.LEVELNAME
FROM OPERATIONALSTRUCTURE AS OS
INNER JOIN HIERLEVELS H
ON H.HIERLEVEL = OS.HIERLEVEL
CROSS APPLY (
SELECT NAME AS PARENTNAME
FROM OPERATIONALSTRUCTURE AS OS2
WHERE OS.PARENTID = OS2.ITEMID
) AS OS2
CROSS APPLY (
SELECT NAME AS HIERNAME
FROM OPERATIONALSTRUCTURE AS OS3
WHERE OS.HIERID = OS3.ITEMID
AND H.HIERID = OS3.HIERID
) AS OS3
ORDER BY OS.HIERID
,OS.PARENTID
,OS.NAME



Long story short, you always need to go back to the same table reading the information you need. I'm afraid there's no way around it.

Hope this helps
Gianluca

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Boby B Jacob
Boby B Jacob
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 77
Thanks Sartori for your efforts.....
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search