I have Question_Master and Assessment_Master table. In question_Master i have questions parent-child relationship. When i answer the parent question, When i filter the answered button filter i need to display parent along with child. When i answer the child question, When i filter the answered button filter i need to display parent along with child. But my issue is, when i answer the both question i'm getting child duplicate.
when answer the parent question it get parent-child:
SELECT a.QM_ID,a.QM_QCM_ID,
a.QM_Question,a.QM_Type,
a.QM_Parent_Id,
c.AM_Answer,
c.AM_Comments
FROM question_master a
INNER JOIN Assessment_master c
ON (c.AM_QM_ID = a.QM_ID
AND c.AM_HNM_ID = %d
AND c.AM_HM_ID = %d
AND c.AM_ASM_Local_Id = %@)
WHERE a.QM_Parent_Id = 0
AND a.QM_Status = 'A'
AND a.QM_QCM_ID = %@
AND a.QM_QRM_Id = %@
UNION
SELECT b.QM_ID,
b.QM_QCM_ID,
b.QM_Question,
b.QM_Type,
b.QM_Parent_Id,
null,
null
FROM question_master b
INNER JOIN Assessment_master d
ON (d. AM_QM_ID = b.QM_Parent_Id
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %@)
WHERE b.QM_Parent_Id != 0
AND b.QM_Status = 'A'
AND b.QM_QCM_ID = %@
AND b.QM_QRM_Id = %@
when i answer the child question it get parent-child:
SELECT a.QM_ID,a.QM_QCM_ID,
a.QM_Question,a.QM_Type,
a.QM_Parent_Id,
c.AM_Answer,
c.AM_Comments
FROM question_master a
INNER JOIN Assessment_master c
ON (c.AM_QM_ID = a.QM_ID
AND c.AM_HNM_ID = %d
AND c.AM_HM_ID = %d
AND c.AM_ASM_Local_Id = %@)
WHERE a.QM_Parent_Id = 0
AND a.QM_Status = 'A'
AND a.QM_QCM_ID = %@
AND a.QM_QRM_Id = %@
UNION
SELECT b.QM_ID,
b.QM_QCM_ID,
b.QM_Question,
b.QM_Type,
b.QM_Parent_Id,
d.AM_Answer,
d.AM_Comments
FROM question_master b
INNER JOIN Assessment_master d
ON (d. AM_QM_ID = b.QM_ID
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %@)
WHERE b.QM_Parent_Id IN (SELECT QM_ID
FROM question_master
WHERE QM_Parent_Id = 0
AND QM_Status = 'A'
AND QM_QCM_ID = %@
AND QM_QRM_Id = %@)
I used combined query from above:
SELECT a.QM_ID,a.QM_QCM_ID,
a.QM_Question,a.QM_Type,
a.QM_Parent_Id,
c.AM_Answer,
c.AM_Comments
FROM question_master a
INNER JOIN Assessment_master c
ON (c.AM_QM_ID = a.QM_ID
AND c.AM_HNM_ID = %d
AND c.AM_HM_ID = %d
AND c.AM_ASM_Local_Id = %@)
WHERE a.QM_Parent_Id = 0
AND a.QM_Status = 'A'
AND a.QM_QCM_ID = %@
AND a.QM_QRM_Id = %@
UNION
SELECT b.QM_ID,
b.QM_QCM_ID,
b.QM_Question,
b.QM_Type,
b.QM_Parent_Id,
null,
null
FROM question_master b
INNER JOIN Assessment_master d
ON (d. AM_QM_ID = b.QM_Parent_Id
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %@)
WHERE b.QM_Parent_Id != 0
AND b.QM_Status = 'A'
AND b.QM_QCM_ID = %@
AND b.QM_QRM_Id = %@
UNION
SELECT b.QM_ID,
b.QM_QCM_ID,
b.QM_Question,
b.QM_Type,
b.QM_Parent_Id,
d.AM_Answer,
d.AM_Comments
FROM question_master b
INNER JOIN Assessment_master d
ON (d. AM_QM_ID = b.QM_ID
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %@)
WHERE b.QM_Parent_Id IN (SELECT QM_ID
FROM question_master
WHERE QM_Parent_Id = 0
AND QM_Status = 'A'
AND QM_QCM_ID = %@
AND QM_QRM_Id = %@)
But here i'm getting extra child duplicate value. When i use LEFT it getting all the child question, if i use EXCEPT then answer the parent question it don't getting parent-child relation.
Question_Master:
QM_ID QM_QRM_ID QM_LCM_ID QM_QCM_ID QM_Question QM_Parent_Id
432 5 19 1 question_parent 0
433 5 19 1 question_child 432
434 5 19 1 question_child1 432
Assessment_Master:
AM_ID AM_UM_ID AM_ASM_Local_Id AM_QM_ID AM_Answer AM_Comments AM_HNM_ID
1 8 1 433 NULL testing 1
If i answer the child question 433 and i'm getting only parent with 433 child. I'm not getting 434 question.
a.QM_ID a.QM_QCM_ID a.QM_Question a.QM_Parent_Id c.AM_Answer c.AM_Comments
432 1 question_parent 0 Null NULL
433 1 question_child 432 null value