473,394 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Rows on the fly and an outer join (use a lateral statement?)

I have an employee table with two columns, one named login, the other
named otherdata.

I have a list of login values, some of which do not exist in the
employee table.

I want to fetch the data in the employee.otherdata column and return
nulls when there is no match.

....

This is sort of what I want:

select e.login,e.otherdata from employee e
left outer join (select 'EEEEEE01' from sysibm.sysdummy1
union select 'AAAAAA01' from sysibm.sysdummy1
union select 'BBBBBB01' from sysibm.sysdummy1
) b
on b(1)=e.login
The problem with this statement is that the creation of the b table is
very slow (there are typically 100 login values), that b(1) is a syntax
error (there wasn't a way to assign the column "login" to the union).
Is this a job for a lateral statement?
I'm using DB2 8.2.0.

Thanks.

Jun 19 '06 #1
5 3111
This works, but is there a better way to create the b table?

Thanks.

select e.login,e.otherdata from employee e
left outer join (select 'EEEEEE01' login from sysibm.sysdummy1
union select 'AAAAAA01' login from sysibm.sysdummy1
union select 'BBBBBB01' login from sysibm.sysdummy1
) b
on b.login=e.login

Jun 19 '06 #2
gi*******************@yahoo.com wrote:
I have an employee table with two columns, one named login, the other
named otherdata.

I have a list of login values, some of which do not exist in the
employee table.

I want to fetch the data in the employee.otherdata column and return
nulls when there is no match.

...

This is sort of what I want:

select e.login,e.otherdata from employee e
left outer join (select 'EEEEEE01' from sysibm.sysdummy1
union select 'AAAAAA01' from sysibm.sysdummy1
union select 'BBBBBB01' from sysibm.sysdummy1
) b
on b(1)=e.login
The problem with this statement is that the creation of the b table is
very slow (there are typically 100 login values), that b(1) is a syntax
error (there wasn't a way to assign the column "login" to the union).
Is this a job for a lateral statement?
I'm using DB2 8.2.0.

OK, lets roll this one up backwards, there is a lot to be learned here:
* b(1) Nice try at syntax, but DB2 will look for a
function b(<number>) here. If any b.1 *shudder* would be it.
You want to specify the column list:
... join (.....) as b(c1, c2, ...) on b.c1 = ...

* Alternatively you can name the columns in the select list of the
union.
The column names get inherited if ALL matching columns agree on
the name.
SELECT 1 AS X, 2 AS Z FROM T
UNION ALL
SELECT 3 AS Y, 4 AS Z FROM T
=> (<noname>, "Z")

* You likely want UNION ALL and not UNION. UNION eliminates duplicates.
An expensive undertaking. In many, many cases where UNION is used the
developer "meant" UNION ALL. If I could turn back time I'd fight to
make: UNION == UNION ALL and folks would have to write UNION DISTINCT.
(or disallow UNION without modifier to begin with....)
* DB2 for LUW supports the VALUES clause. That is there is no need to go
after "funny" single row tables or glue together UNION ALLs:

select e.login,e.otherdata from employee e
left outer join (VALUES ('EEEEEE01'),
('AAAAAA01'),
('BBBBBB01')) AS B(login)
on b.login=e.login

If you need compatibility with DB2 for zOS use:
select e.login,e.otherdata from employee e
left outer join (select 'EEEEEE01' from sysibm.sysdummy1
union all select 'AAAAAA01' from sysibm.sysdummy1
union all select 'BBBBBB01' from sysibm.sysdummy1
) b(login)
on b(1)=e.login

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jun 19 '06 #3
Thanks for all the solutions Serge.

This was SWEET!

select e.login,e.otherdata from employee e
left outer join (VALUES ('EEEEEE01'),
('AAAAAA01'),
('BBBBBB01')) AS B(login)
on b.login=e.login

Serge Rielau wrote:
gi*******************@yahoo.com wrote:
I have an employee table with two columns, one named login, the other


Jun 19 '06 #4

gi*******************@yahoo.com wrote:
Thanks for all the solutions Serge.

This was SWEET!

select e.login,e.otherdata from employee e
left outer join (VALUES ('EEEEEE01'),
('AAAAAA01'),
('BBBBBB01')) AS B(login)
on b.login=e.login

I haven't run across that way of doing it before - very nice.

One other option - maybe not as nice - is to use common table
expressions:

with B(login) as (VALUES ('EEEEEE01'),
('AAAAAA01'),
('BBBBBB01'))
select e.login,e.otherdata from employee e
left outer join b on b.login = e.login

As an aside, do these queries actually return what you want? I would
expect you should get the entire employee table with full values as
defined in the employee table. If you define additional logins in the
'B' table, I wouldn't expect them to be returned in this query. Unless
you switched the order (...from b left outer join employee e...).

-Chris

Jun 19 '06 #5
Yes, I switched the order, well actually, I changed it to a right outer
join.

I'm getting a bunch of login values from LDAP in a Java program and
then
I'm fetching addition information for the users from a database table.
I didn't
want to use an in statement and execute if statements on the results to
match things up.

As an aside, do these queries actually return what you want? I would
expect you should get the entire employee table with full values as
defined in the employee table. If you define additional logins in the
'B' table, I wouldn't expect them to be returned in this query. Unless
you switched the order (...from b left outer join employee e...).

-Chris


Jun 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: YONETANI Tomokazu | last post by:
Hi. You can use the following SQL to construct rows with column names on the fly, rather than from an existing table like sysibm.sysdummy1: SELECT * FROM TABLE ( VALUES (0, 1, 2), (3, 4, 5), (6,...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
4
by: cbrichards via SQLMonster.com | last post by:
I have a stored procedure that will execute with less than 1,000 reads one time (with a specified set of parameters), then with a different set of parameters the procedure executes with close to...
5
by: Sascha.Moellering | last post by:
Hi, I receive the error code SQL0338N if I try to compile this statement (part of the statement): .... left outer join lateral (SELECT * FROM LIZSYSABA.VWZL0359TBS WHERE tbs_name = CASE WHEN...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.