{"id":21968,"date":"2025-01-21T23:48:50","date_gmt":"2025-01-22T05:48:50","guid":{"rendered":"http:\/\/www.designandexecute.com\/designs\/?p=21968"},"modified":"2025-01-21T23:48:52","modified_gmt":"2025-01-22T05:48:52","slug":"managing-self-joins-with-multiple-context-in-neo4j","status":"publish","type":"post","link":"https:\/\/www.designandexecute.com\/designs\/managing-self-joins-with-multiple-context-in-neo4j\/","title":{"rendered":"Managing Self Joins with Multiple Context in Neo4J"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Example of <strong>Cypher Query using Relationship <\/strong> design pattern.<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Query:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>MATCH (manager:Employee)&lt;-[rel:PRIMARY_REPORTS_TO|DOTTED_REPORTS_TO]-(e:Employee)\nWHERE manager.name = \"Alice\"\nRETURN e.name AS employee, \n       type(rel) AS relationship_type, \n       rel.role AS role, \n       rel.start_date, \n       rel.end_date;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Sample Graph Data:<\/h4>\n\n\n\n<p>Assume we have the following graph:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>Bob<\/code> reports to <code>Alice<\/code> as the <strong>primary manager<\/strong>.<\/li><li><code>Charlie<\/code> reports to <code>Alice<\/code> as a <strong>dotted-line report<\/strong> for a project.<\/li><li><code>Diana<\/code> reports to <code>Alice<\/code> as a <strong>dotted-line report<\/strong> for mentorship.<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Query Results:<\/h4>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>employee<\/th><th>relationship_type<\/th><th>role<\/th><th>start_date<\/th><th>end_date<\/th><\/tr><\/thead><tbody><tr><td>Bob<\/td><td>PRIMARY_REPORTS_TO<\/td><td>NULL<\/td><td>2023-01-01<\/td><td>NULL<\/td><\/tr><tr><td>Charlie<\/td><td>DOTTED_REPORTS_TO<\/td><td>Project Lead<\/td><td>2023-02-01<\/td><td>2024-01-31<\/td><\/tr><tr><td>Diana<\/td><td>DOTTED_REPORTS_TO<\/td><td>Mentor<\/td><td>2023-03-15<\/td><td>2023-12-31<\/td><\/tr><\/tbody><\/table>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>type(rel) AS relationship_type<\/code><\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>\n<strong>Purpose of <code>type(rel)<\/code><\/strong>:\n<ul><li><code>type(rel)<\/code> retrieves the name of the relationship type for each match.<\/li><li>In this query, it can return either:\n<ul><li><code>PRIMARY_REPORTS_TO<\/code><\/li><li><code>DOTTED_REPORTS_TO<\/code><\/li><\/ul>\n<\/li><li>This is useful when multiple types of relationships are being matched in the same query.<\/li><\/ul>\n<\/li><li>\n<strong>How It Works in the Query<\/strong>:\n<ul><li>The query matches any <code>rel<\/code> of type <code>PRIMARY_REPORTS_TO<\/code> or <code>DOTTED_REPORTS_TO<\/code> between <code>Alice<\/code> (the manager) and her employees.<\/li><li>The <code>type(rel)<\/code> function determines whether the relationship is a primary or dotted-line report.<\/li><\/ul>\n<\/li><li>\n<strong>Practical Use<\/strong>:\n<ul><li>It allows you to differentiate between primary and dotted-line reports in the result set.<\/li><li>For instance, in the results above:\n<ul><li>Bob\u2019s relationship is labeled as <code>PRIMARY_REPORTS_TO<\/code>.<\/li><li>Charlie and Diana\u2019s relationships are labeled as <code>DOTTED_REPORTS_TO<\/code>.<\/li><\/ul>\n<\/li><\/ul>\n<\/li><li>\n<strong>Handling NULL Values<\/strong>:\n<ul><li>In relationships like <code>PRIMARY_REPORTS_TO<\/code>, there may not be a <code>role<\/code> property (e.g., Bob has no <code>role<\/code> in his primary reporting line). This is reflected as <code>NULL<\/code> in the <code>role<\/code> column of the results.<\/li><\/ul>\n<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why This Query is Powerful<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li> <strong>Insight Across Multiple Reporting Lines<\/strong>: <ul><li>Combines both primary and dotted-line reporting relationships into a single result set.<\/li><li>Useful for identifying all employees managed by a specific manager, regardless of the type of reporting. <\/li><\/ul><\/li><li> <strong>Dynamic Role Attribution<\/strong>: <ul><li>The <code>rel.role<\/code> property allows capturing contextual information about the dotted-line relationships (e.g., &#8220;Project Lead&#8221; or &#8220;Mentor&#8221;). <\/li><\/ul><\/li><li> <strong>Time-Bounded Relationships<\/strong>: <ul><li>The <code>start_date<\/code> and <code>end_date<\/code> fields allow filtering based on active reporting periods. For example, you could modify the query to <strong>find employees who are currently reporting to Alice<\/strong>: <code>MATCH (manager:Employee)&lt;-[rel:PRIMARY_REPORTS_TO|DOTTED_REPORTS_TO]-(e:Employee) WHERE manager.name = \"Alice\" AND (rel.end_date IS NULL OR date(rel.end_date) >= date()) RETURN e.name AS employee, type(rel) AS relationship_type, rel.role, rel.start_date, rel.end_date; <\/code>  <\/li><\/ul><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Example of Cypher Query using Relationship design pattern. Query: Sample Graph Data: Assume we have the following graph: Bob reports to Alice as the primary manager. Charlie reports to Alice as a dotted-line report for a project. Diana reports to Alice as a dotted-line report for mentorship. Query Results: employee relationship_type role start_date end_date Bob [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":21496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-21968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bi-dashboards-analytics"],"jetpack_featured_media_url":"https:\/\/www.designandexecute.com\/designs\/wp-content\/uploads\/2024\/12\/neo4j_logo-facebook-3405355569.png","_links":{"self":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts\/21968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/comments?post=21968"}],"version-history":[{"count":4,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts\/21968\/revisions"}],"predecessor-version":[{"id":21972,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts\/21968\/revisions\/21972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/media\/21496"}],"wp:attachment":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/media?parent=21968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/categories?post=21968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/tags?post=21968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}