Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Self Joins in SQL Server 2005 Expand / Collapse
Author
Message
Posted Wednesday, August 8, 2012 3:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, December 1, 2014 7:30 AM
Points: 11, Visits: 69
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
Post #1341757
Posted Wednesday, August 8, 2012 4:31 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:13 AM
Points: 4,654, Visits: 11,116
A bit vague, can you clarify please?
Do you have an example?


--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Post #1341776
Posted Wednesday, August 8, 2012 4:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, December 1, 2014 7:30 AM
Points: 11, Visits: 69
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
Post #1341783
Posted Wednesday, August 8, 2012 4:58 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:13 AM
Points: 4,654, Visits: 11,116
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
Post #1341787
Posted Wednesday, August 8, 2012 5:17 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, December 1, 2014 7:30 AM
Points: 11, Visits: 69
Thanks Sartori for your efforts.....
Post #1341793
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse