Home : Products : Hach WIMS : Hach WIMS Client : Documentation : Tips and Tricks : Location Tree Example Queries
Q14295 - INFO: Location Tree Example Queries

Self Join - Show all locations that are not a parent to another location and show their parent.

SELECT L.LOCATION,L.PARENTID,P.LOCATION as PARENTLOC
FROM LOCATION L INNER JOIN LOCATION  P ON L.PARENTID=P.LOCID
WHERE L.LOCID NOT IN (SELECT DISTINCT PARENTID FROM LOCATION WHERE PARENTID IS NOT NULL)

 

Recusive Join to show location path.

WITH CTE (LOCID, LOCATION,RECORDER) AS
(
    SELECT LOCID, cast(location as varchar(max)) as locpath,RECORDER
    FROM Location g
    WHERE parentid IS NULL
    UNION ALL
    SELECT g2.LOCID, Cast(CTE.location + '\' + g2.Location as Varchar(max)),g2.RECORDER
    FROM Location g2
    JOIN CTE ON g2.PARENTID = cte.LocId
)
SELECT   * FROM   CTE
ORDER BY RECORDER

 

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 1/23/2015 2:31 PM.
Last Modified on 1/23/2015 3:07 PM.
Last Modified by Scott Dorner.
Article has been viewed 2767 times.
Rated 4 out of 10 based on 2 votes.
Print Article
Email Article