if i make a union between multiple queries (4 for example) and i get the following
result :
id name num
13 task1 0
13 task1 7102
How to get only the unique where num is greater than 0 if there are more than one record with the same id in the union statement .
foreach
SELECT DISTINCT a.task_code,
a.task_name ,
0 AS cand_num INTO ll_task_code ,
ls_task_name,
ll_cand_num
FROM rmtask a,
rmtaskstate b,
rmstateuser c
WHERE (a.task_code = b.task_code)
AND (b.state_code = c.state_code)
AND (c.emp_num = al_emp_num)
AND (al_new_flag = 0
OR (al_new_flag = 1
AND b.new_flag = 1))
UNION --candidate
SELECT DISTINCT a.task_code,
a.task_name ,
cand.emp_num
FROM rmtask a,
rmtaskstate b,
rmstateuser c ,
rmcandidate cand
WHERE (a.task_code = b.task_code)
AND (b.state_code = c.state_code)
AND (c.emp_num = cand.emp_num)
AND (al_new_flag = 0
OR (al_new_flag = 1
AND b.new_flag = 1))
AND (cand.task_code = b.task_code)
AND (cand.emp_num_candidate = al_emp_num)
AND (cand.from_date <= DATE(CURRENT))
AND (cand.to_date >= DATE(CURRENT)
OR cand.to_date IS NULL)
UNION
SELECT DISTINCT a.task_code,
a.task_name ,
0 AS cand_num
FROM rmtask a,
rmtaskstate b,
rmstategroup c
WHERE (a.task_code = b.task_code) )
UNION --candidate
SELECT DISTINCT a.task_code,
a.task_name ,
cand.emp_num
FROM rmtask a,
rmtaskstate b,
rmstategroup c ,
rmcandidate cand
WHERE (a.task_code = b.task_code)
AND (b.state_code= c.state_code)
AND (al_new_flag = 0
OR (al_new_flag = 1
AND b.new_flag = 1))
AND (((c.group_type = 0))
OR ((c.group_type = 1)
AND (c.group_code =
(SELECT x.degree_code
FROM hr_l x
WHERE x.emp_num = cand.emp_num
AND x.degree_date =
(SELECT max(xx.degree_date)
FROM hm xx
WHERE xx.emp_num = x.emp_num))))
OR ((c.group_type = 2)
AND (c.group_code =
(SELECT y.title_code
FROM ht y
WHERE y.emp_num = cand.emp_num
AND y.title_date =
(SELECT max(yy.title_date)
FROM ht yy
WHERE yy.emp_num = y.emp_num))))
OR ((c.group_type = 3)
AND (1 = r_boss(cand.emp_num)))
OR ((c.group_type = 4)
AND (0 <
(SELECT count(*)
FROM ht x
WHERE x.emp_num = cand.emp_num
AND x.perm_flag = 1)))
OR ((c.group_type = 5)
AND (0 <
(SELECT count(*)
FROM hm x
WHERE x.emp_num = cand.emp_num
AND x.vac_flag = 1)))
OR ((c.group_type = 6)
AND (0 <
(SELECT count(*)
FROM hm x
WHERE x.emp_num = cand.emp_num
AND x.mission_flag = 1))))
AND (cand.task_code = b.task_code)
AND (cand.emp_num_candidate = al_emp_num)
AND (cand.from_date <= DATE(CURRENT))
AND (cand.to_date >= DATE(CURRENT)
OR cand.to_date IS NULL) RETURN ll_task_code ,
ls_task_name,
ll_cand_num WITH resume ;
end foreach;
id,name,num, but the SQL seems to usetask_code,task_name, andcand_num.So, write the query like that:
Clearly, you can add the other alternative UNION branches inside the sub-query.