In Neo4j, I have relation (p:Person)-[:HAS_DOCUMNET]->(d:Document)
and Required only 1 row data per person (personId and documentType) I required to develop query output on the basis of below priority condition
1)If documentType="Passport" is present then should come with personId.(1st Priority) 2)If documentType="VoterCard" is present then should come with personeId.(If Passport is not present)
3)If documentType="PanCard" is present then should come withe personId.(If VoterId is not present)
Data Present like below:
(P1:Person)-[:HAS_Document]->("Passpot","VoterCard",PanCard)`
(P2:Person)-[:HAS_Document]->("VoterCard","PanCard")`
(P3:Person)-[:HAS_Document]->("PanCard","AadharCard","VoterCard")
(P4:Person)-[:HAS_Document]->("PanCard")
Output should be like (Only one document required against one person based on priority):
PName Doc.Type
------- -----------
P1 "Passport"
P2 "VoterCard"
P3 "VoterCard"
P4 "PanCard"
I tried below query which is not working
I am trying below query which is not working:
Match(p:Person)-[:HAS_DOCUMNET]->(d1:Document{Doc_Type:"Passport"})
Optional Match(p:Person)-[:HAS_DOCUMNET]->(d2:Document{Doc_Type:"VoterCard"})
OptionalMatch(p:Person)-[:HAS_DOCUMNET]->(d3:Document{Doc_Type:"PanCard"})
Return p.PName as PName,coalesce(d1.Doc_Type,coalesce(d2.Doc_Type,coalesce(d3.Doc_Type))) as Doc.Type