Unix Technical Forum

need header values

This is a discussion on need header values within the DB2 forums, part of the Database Server Software category; --> How to get the header values in the sql with union. For example, select deptno as dep,deptname as dname,location ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 04:47 AM
hikums@gmail.com
 
Posts: n/a
Default need header values

How to get the header values in the sql with union.

For example,
select deptno as dep,deptname as dname,location as loc,mgrno as mgr
from dept
union
select empno,empname,'Employee','' from emp

I need output with
header:
dep dname loc mgr
1 mkt 4 34
2 sls 4 33
34 James Employee 45

Thank for your time.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 04:47 AM
Rhino
 
Posts: n/a
Default Re: need header values


<hikums@gmail.com> wrote in message
news:1116344807.003195.286310@z14g2000cwz.googlegr oups.com...
> How to get the header values in the sql with union.
>
> For example,
> select deptno as dep,deptname as dname,location as loc,mgrno as mgr
> from dept
> union
> select empno,empname,'Employee','' from emp
>
> I need output with
> header:
> dep dname loc mgr
> 1 mkt 4 34
> 2 sls 4 33
> 34 James Employee 45
>
> Thank for your time.
>


I was surprised that your query didn't work just the way it was so I tried
it and confirmed that it failed to put the desired headings at the top of
the columns in DB2 for Windows/Unix/Linux, Version 8 (Fixpack 8). (By the
way, I had to change to table names from 'emp' and 'dept' to 'employee' and
'department' so I assume you are on an older version of DB2 or on a
different operating system.)

I got the desired headings to appear by putting the 'as' expressions in
_both_ queries:
select deptno as dep,deptname as dname,location as loc,mgrno as mgr
from department
union
select empno as dep,lastname as dname,'Employee' as loc,'' as mgr from
employee

Rhino


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 04:47 AM
hikums@gmail.com
 
Posts: n/a
Default Re: need header values

I tried my query with putting "as " in each of the select. Just putting
it on one select does not work.

Thanks Rhino.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 04:47 AM
Tonkuma
 
Posts: n/a
Default Re: need header values

You should specify same renamed column names explicitly for both SELECT
list.
For example,
select deptno as dep,deptname as dname,location as loc,mgrno as mgr
from dept
union
select empno as dep,empname as dname,'Employee' as loc,'' as mgr from
emp

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 04:47 AM
Knut Stolze
 
Posts: n/a
Default Re: need header values

Tonkuma wrote:

> You should specify same renamed column names explicitly for both SELECT
> list.
> For example,
> select deptno as dep,deptname as dname,location as loc,mgrno as mgr
> from dept
> union
> select empno as dep,empname as dname,'Employee' as loc,'' as mgr from
> emp


Alternatively you could do this:

SELECT *
FROM ( SELECT deptno, deptname, location, mgrno
FROM dept
UNION
SELECT empno, empname, 'Employee', ''
FROM emp ) AS t(dep, dname, loc, mgr)

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 04:48 AM
hikums@gmail.com
 
Posts: n/a
Default Re: need header values

Thanks Knut, that is a cool way of doing it..

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:08 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com