You are seeing the duplicate value because you are making CROSS JOIN through your query. This could be easily identified if you would add <mn id="39af9fa1" name="BS" dt="30" df="34" ty="38" /> under the <ghi att="ghi1"> tag. Your result would would increase from 3*1=3 rows to 3*2=6 rows.
For the solution, you need to specify the joining keys and tweak your code to something like
SELECT Z.value('../../@id[1]', 'VARCHAR(64)') AS ID
, Z.value('@id[1]', 'VARCHAR(64)') AS Tm
, Z.value('@name[1]', 'VARCHAR(64)') AS Pl
, Z.value('@dt[1]', 'int') AS defDT
, Z.value('@gl[1]', 'int') AS defGL
, COALESCE(X.value('@dt[1]', 'int'), 0) AS abcDT
, COALESCE(X.value('@df[1]', 'int'), 0) AS abcDF
, COALESCE(X.value('@ty[1]', 'int'), 0) AS abcTY
FROM @x.nodes('//abc/def/jkl/mn') n ( Z )
LEFT JOIN @x.nodes('//abc/def/ghi/mn') AS d ( X )
ON d.X.value('@id', 'VARCHAR(64)') = n.Z.value('@id', 'VARCHAR(64)')
AND d.X.value('@id', 'varchar(64)') IN ( '0742d2ea', '4bf3ba2f',
'c4c93a2d' )
WHERE n.Z.value('@id', 'varchar(64)') IN ( '0742d2ea', '4bf3ba2f',
'c4c93a2d' )
Hope it helps.
answered
Apr 16 '12 at 06:23 AM
Usman Butt
13.8k
●
6
●
8
●
14
It might be helpful if you indicated the answer you were expecting, or wanting...