Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:45:55+00:00 2026-06-18T02:45:55+00:00

if i make a union between multiple queries (4 for example) and i get

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:45:57+00:00Added an answer on June 18, 2026 at 2:45 am
    1. Use JOIN notation instead of comma lists in the FROM clause.
    2. We rally don’t need to see all your query to give an answer; two parts to the union would be sufficient.
    3. You’ve not mentioned the version of Informix you’re using, which can make a difference.
    4. It would be easier if your example output tied in with the SQL you write. Your sample output uses id, name, num, but the SQL seems to use task_code, task_name, and cand_num.
    5. It would appear that you want the maximum candidate number for a given task code and task name.

    So, write the query like that:

    SELECT u.task_code, u.task_name, MAX(u.cand_num)
      FROM (SELECT DISTINCT a.task_code, a.task_name, 0 AS cand_num
              FROM rmtask a
              JOIN rmtaskstate b ON a.task_code = b.task_code
              JOIN rmstateuser c ON b.state_code = c.state_code
             WHERE (c.emp_num = al_emp_num)
               AND (al_new_flag = 0 OR (al_new_flag = 1 AND b.new_flag = 1))
            UNION
            SELECT DISTINCT a.task_code, a.task_name, cand.emp_num
              FROM rmtask a
              JOIN rmtaskstate b ON a.task_code = b.task_code
              JOIN rmstateuser c ON b.state_code = c.state_code
              JOIN rmcandidate cand ON c.emp_num = cand.emp_num
             WHERE (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)
           ) AS u
     GROUP BY u.task_code, u.task_name;
    

    Clearly, you can add the other alternative UNION branches inside the sub-query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to combine multiple EllipseGeometry to make a union between them,
I am trying to make a Union between two tables, and I get the
hi I have the following code and would like to only make each union
I'm using Drupal 7 and I have to make a union on multiple tables.
I have this queries, but I need to make a Union for all. But
I make a bot program and i want to send some keyboard keys between
SOLUTION Make sure in the plist that the storyboard name is listed as the
I have a table T1, it contains a NAME value (not unique), and a
I want to make a mysql union search. My purpose is: my total results
I'm trying to make sense of a puzzling behavior with git union merge. To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.