473,408 Members | 2,477 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,408 software developers and data experts.

Transform SubSelect in OUTER JOIN

Hi,

maybe I'm simply to dump but I could not transform this SQL-Statment
which uses a Sub-select and create on that uses an OUTER JOIN.

--------------------------8<--------------------------
SELECT
p_id,
CONCAT( " hist. ", CONCAT( UPPER(p_surname), CONCAT( ',',
p_givenname ) ) )
FROM
persons p,
employee_contracts e
WHERE
p.delmark = 0
AND e.delmark = 0
AND e.ec_fk_person = p.p_id
AND p_is_employee = 1
AND e.ec_end_date <= NOW()
AND p_id NOT IN
(
SELECT DISTINCT
p_id
FROM
persons p,
employee_contracts e
WHERE
p.delmark = 0
AND e.delmark = 0
AND e.ec_fk_person = p.p_id
AND p_is_employee = 1
AND ( e.ec_end_date >= NOW() OR e.ec_end_date IS NULL )

)
--------------------------8<--------------------------

Thanks for your help. I cannot switch to mysql-4.1 and that's why I need
this working without sub-selects.

Tom
Jul 23 '05 #1
1 1538
Tom Schindl wrote:
...I could not transform this SQL-Statment
which uses a Sub-select and create one that uses an OUTER JOIN.
....troublesome query omitted...

You might have already found a solution, since your original posting was
four days ago, but try something like this:

SELECT p.p_id, CONCAT( " hist. ", CONCAT( UPPER(p.p_surname), CONCAT( ',', p.p_givenname ) ) )

FROM persons AS p
INNER JOIN employee_contracts AS e
ON p.p_id = e.ec_fk_person
LEFT OUTER JOIN employee_contracts AS e2
ON (p.p_id = e2.ec_fk_person AND e2.delmark = 0
AND (e2.ec_end_date >= NOW() OR e2.ec_end_date IS NULL) )
WHERE p.delmark = 0 AND e.delmark = 0 AND p.p_is_employee = 1
AND e.ec_end_date <= NOW()
AND e2.ec_fk_person IS NULL;

Caveats: this is not tested;
also, it assumes that ec_fk_person is a NOT NULL field, so a NULL state
could only be true if the outer join had not been satisfied.

Regards,
Bill K.
Jul 23 '05 #2

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

Similar topics

8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
1
by: Magnus | last post by:
Hi, I have previously worked mostly with Sql Server and wonder how to write a subselect query in mysql 4.0. What i would like to do is something like: select a.name, b.isbn from author a...
6
by: Greg Stark | last post by:
So I have a query in which some of the select values are subqueries. The subqueries are aggregates so I don't want to turn this into a join, it would become too complex and postgres would have...
3
by: Neil Zanella | last post by:
Hello, I would like to ask the about the following... PostgreSQL allows tables resulting from subselects to be renamed with an optional AS keyword whereas Oracle 9 will report an error...
4
by: dtwalter | last post by:
Is it possible to ORDER BY a SubSelect? I don't see why it wouldn't be, but I'm having some trouble. Hopefully it's just a simple error in syntax and somebody can tell me. Here's what I'm trying...
1
by: Marco Lazzeri | last post by:
I'd like to reference values returned by a subselect in the same SELECT query. Example: SELECT id, ( SELECT COUNT(*) FROM second ) AS value_to_reference, ( value_to_reference + 1 ) AS...
2
by: Morten K. Poulsen | last post by:
(re-post) Dear list, Please let me know if this is not the list to ask this kind of question. I am trying to optimize a query that joins two relatively large (750000 rows in each) tables....
6
by: Sebastien | last post by:
I have the following statement which I run successfully in... 1 hour 10 minutes. SELECT a.tsgicd as ACCT_ID, a.tsa5cd as SEC_ID, CASE WHEN (SUBSTRING(a.tsgicd, 6, 1) = 'R' or...
4
by: johnfaulkner | last post by:
Hi, I am trying to perform a single select of data from 2 tables, table A and table B. Table B may have none, one or many corresponding rows. If table B has no corresponding rows then table B...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.