So I am running into a problem with getting the right data from my query.
I have the following table (Person):
Name | xpath | value
Derek | /body/torso/arm | left
Derek | /body/torso/arm | right
Derek | /body/torso/neck | Head
Ron | /body/torso/neck | Head
Ron | /body/torso/arm | left
So basically the query results i was trying to get were to show the differences (whats missing between the two people).
results:
Name1 | xpath1 | value1 | Name2 | xpath2 | value2
Derek | /body/torso/arm | right | Ron | NULL | NULL
I would even be fine with getting the following back as well
results:
Name1 | xpath1 | value1 | Name2 | xpath2 | value2
Derek | /body/torso/arm | right | Ron | NULL | NULL
Derek | /body/torso/arm | left | Ron | /body/torso/arm | left
Derek | /body/torso/neck | Head | Ron | /body/torso/neck | Head
The query i was using was:
SELECT P.Name , P.xpath, P.value, P1.Name, P2.xpath, P3.value
FROM Person as P
LEFT OUTER JOIN
Person as P2 ON P.xpath = P2.xpath
WHERE
P.Name = "Derek"
AND P2.Name = "Ron"
The results I keep getting no matter what i try is basically the overlapping data but never what i am actually looking for, seems I am missing something simple or just plainly doing it wrong. Any suggestions? Ultimately i am going to run this in a SPROC but it would be nice to be able to handle multiple names not just 2, but if i had, Derek,Ron,John,Dawn, etc..
Give this a shot:
SQLFiddle Demo
If you wanted to compare Derek to multiple names, you can do a
CROSS JOINof all names to compare to (so that the names can appear in the result set), andLEFT JOINthe same table usingxpath,value, and also the names specified in theCROSS JOIN:SQLFiddle Demo
And if you wanted to have multiple names on the left side of the comparison (not just “Derek”), just change
to
in the above query.
SQLFiddle Demo
EDIT: Just to take it even further:
Compare all names to all names: