Connecting Tech Pros Worldwide Help | Site Map

Return union result in one row?

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: 4 Weeks Ago
Hi folks,

I want to union two tables into one row per empno

SELECT EMPNO, EMAIL , ' '
FROM EMP_T

UNION ALL
SELECT EMPNO, ' ', MGREMAIL
FROM MGR_T

tables look like this:

EMP_T

EMPNO EMAIL
------- ----------
0001 emp1@123.com
0002 emp2@123.com
0003 emp3@123.com

MGR_T

EMPNO MGREMAIL
------- -----------------------------
0001 mgr1@123.com
0002 mgr2@123.com
0004 mgr3@123.com
0003 mgr4@123.com

I want the result to look like this:

EMPNO email mgremail
------- -------------------- --------------------
0001 emp1@123.com mgr1@123.com
0002 emp2@123.com mgr2@123.com
0004 mgr3@123.com
0003 emp3@123.com mgr4@123.com

Any suggestions ?
Thanks
Member
 
Join Date: Aug 2007
Posts: 51
#2: 1 Week Ago

re: Return union result in one row?


Expand|Select|Wrap|Line Numbers
  1. select empno,
  2.               max(email) as email,
  3.               max(mgremail) as mgremail
  4.      from (select empno, email , ' ' as mgremail
  5.                    from emp_t
  6.                  union all
  7.                  select empno, ' ' as email, mgremail
  8.                     from mgr_t) x
  9.    group by empno
  10.  
Newbie
 
Join Date: Oct 2009
Posts: 2
#3: 1 Week Ago

re: Return union result in one row?


Nice work, many thanks
Reply