如果我在多个查询(例如 4 个)之间进行联合,我得到以下信息
结果 :
id name num
13 task1 0
13 task1 7102
如果 union 语句中有多个具有相同 id 的记录,如何仅获取num 大于 0的唯一记录。
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;