473,806 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.otherd ata column and return
nulls when there is no match.

....

This is sort of what I want:

select e.login,e.other data from employee e
left outer join (select 'EEEEEE01' from sysibm.sysdummy 1
union select 'AAAAAA01' from sysibm.sysdummy 1
union select 'BBBBBB01' from sysibm.sysdummy 1
) 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 3132
This works, but is there a better way to create the b table?

Thanks.

select e.login,e.other data from employee e
left outer join (select 'EEEEEE01' login from sysibm.sysdummy 1
union select 'AAAAAA01' login from sysibm.sysdummy 1
union select 'BBBBBB01' login from sysibm.sysdummy 1
) b
on b.login=e.login

Jun 19 '06 #2
gi************* ******@yahoo.co m 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.otherd ata column and return
nulls when there is no match.

...

This is sort of what I want:

select e.login,e.other data from employee e
left outer join (select 'EEEEEE01' from sysibm.sysdummy 1
union select 'AAAAAA01' from sysibm.sysdummy 1
union select 'BBBBBB01' from sysibm.sysdummy 1
) 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.other data 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.other data from employee e
left outer join (select 'EEEEEE01' from sysibm.sysdummy 1
union all select 'AAAAAA01' from sysibm.sysdummy 1
union all select 'BBBBBB01' from sysibm.sysdummy 1
) 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.other data from employee e
left outer join (VALUES ('EEEEEE01'),
('AAAAAA01'),
('BBBBBB01')) AS B(login)
on b.login=e.login

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


Jun 19 '06 #4

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

This was SWEET!

select e.login,e.other data 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.other data 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
5011
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, 7, 8) ) X(foo, bar, baz); or WITH X(foo, bar, baz) AS ( VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8) ) SELECT * FROM X;
6
13769
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 t1 ON relevant_stuff WHERE (lots of conditions) After re-reading the relevant pat ofVol 1 of the SQL Reference I am unablee to see how this is possible.
4
7734
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 500,000 reads (according to Profiler). In comparing the execution plans, they are the same, except for the actual and estimated number of rows. When the proc runs with parameters that produce reads that are less than 1,000 the actual and...
5
15521
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 MC.type_txt = 'ZAB' THEN 'BII' ELSE 'STD' END) AS TB1 on CASE WHEN MC.fixed_date_dat IS NULL THEN cast('01.01.2007' as date) + MC.rel_shift_NR DAY ELSE MC.fixed_date_dat END
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10364
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.