473,396 Members | 1,755 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,396 software developers and data experts.

Left Join query

To build a grid, all the distinct rows from T1 are required,
and only those from T2 which fall btn 2003-09-11 and 2003-09-18.
In the following example:
A is included because it is within the date range
C is inclucded because the date is null
B is excluded because it is outside the date range

Any suggestions on how to get the desired result would be greatly
appreciaed!

T1 T2

ID ID DATES

A A 2003-09-10

B B 2003-09-10

C A 2003-09-11

Desired Result

A 2003-09-11

B

C

Select T1.ID from T1 left join T2 on T1.ID = T2.ID

where (T2.DATES>=' 2003-09-11' and T2.DATES<=' 2003-09-18') or T2.DATES
is null

Result

A 2003-09-11

C


Jul 19 '05 #1
6 4779
BDR
Afaik, once you've included a column to be listed in your statement, you
can't 'selectively' eliminate it (or force a display null) in a selected
row result. The way I work around this is do a select statement for all
distinct rows in T1, then another below it in programming code to select
the desired date range output. Not a subselect, but two selects using
two simultaneous connections to mysql. (But if you're using mysql>4 I
think a subselect would certainly do the trick though).

IE:
Select Distinct T1.ID from T1
while (T1.ID)
print t1.id...
Select T2.DATES where T2.ID=T1.ID AND ((T2.DATES>='2003-09-11' and

T2.DATES<='2003-09-18') or T2.DATES is null)
print T2.DATES...

Something on this order, depending on what your pgming with.

Dave wrote:
To build a grid, all the distinct rows from T1 are required,
and only those from T2 which fall btn 2003-09-11 and 2003-09-18.
In the following example:
A is included because it is within the date range
C is inclucded because the date is null
B is excluded because it is outside the date range

Any suggestions on how to get the desired result would be greatly
appreciaed!

T1 T2

ID ID DATES

A A 2003-09-10

B B 2003-09-10

C A 2003-09-11

Desired Result

A 2003-09-11

B

C

Select T1.ID from T1 left join T2 on T1.ID = T2.ID

where (T2.DATES>=' 2003-09-11' and T2.DATES<=' 2003-09-18') or T2.DATES
is null

Result

A 2003-09-11

C


Jul 19 '05 #2
BDR
Afaik, once you've included a column to be listed in your statement, you
can't 'selectively' eliminate it (or force a display null) in a selected
row result. The way I work around this is do a select statement for all
distinct rows in T1, then another below it in programming code to select
the desired date range output. Not a subselect, but two selects using
two simultaneous connections to mysql. (But if you're using mysql>4 I
think a subselect would certainly do the trick though).

IE:
Select Distinct T1.ID from T1
while (T1.ID)
print t1.id...
Select T2.DATES where T2.ID=T1.ID AND ((T2.DATES>='2003-09-11' and

T2.DATES<='2003-09-18') or T2.DATES is null)
print T2.DATES...

Something on this order, depending on what your pgming with.

Dave wrote:
To build a grid, all the distinct rows from T1 are required,
and only those from T2 which fall btn 2003-09-11 and 2003-09-18.
In the following example:
A is included because it is within the date range
C is inclucded because the date is null
B is excluded because it is outside the date range

Any suggestions on how to get the desired result would be greatly
appreciaed!

T1 T2

ID ID DATES

A A 2003-09-10

B B 2003-09-10

C A 2003-09-11

Desired Result

A 2003-09-11

B

C

Select T1.ID from T1 left join T2 on T1.ID = T2.ID

where (T2.DATES>=' 2003-09-11' and T2.DATES<=' 2003-09-18') or T2.DATES
is null

Result

A 2003-09-11

C


Jul 19 '05 #3
BDR
Hate responding to my own posts... but I didn't stay at a holiday inn
express last night. This one is much easier than I made it sound
originally. (I just finished a monster multi-join prob yesterday that
threw my head for a loop on this simple one that you posted)..

Move your where condition to the on condition...

SELECT T1.ID, T2.DATES
FROM T1
left join T2
on T1.ID = T2.ID
and ((T2.DATES >= ' 2003-09-11' and T2.DATES <= ' 2003-09-18')
or T2.DATES is null)

ID DATES
A 2003-09-11
B NULL
C NULL
BDR wrote:
Afaik, once you've included a column to be listed in your statement, you
can't 'selectively' eliminate it (or force a display null) in a selected
row result. The way I work around this is do a select statement for all
distinct rows in T1, then another below it in programming code to select
the desired date range output. Not a subselect, but two selects using


....snip...

Jul 19 '05 #4
BDR
Hate responding to my own posts... but I didn't stay at a holiday inn
express last night. This one is much easier than I made it sound
originally. (I just finished a monster multi-join prob yesterday that
threw my head for a loop on this simple one that you posted)..

Move your where condition to the on condition...

SELECT T1.ID, T2.DATES
FROM T1
left join T2
on T1.ID = T2.ID
and ((T2.DATES >= ' 2003-09-11' and T2.DATES <= ' 2003-09-18')
or T2.DATES is null)

ID DATES
A 2003-09-11
B NULL
C NULL
BDR wrote:
Afaik, once you've included a column to be listed in your statement, you
can't 'selectively' eliminate it (or force a display null) in a selected
row result. The way I work around this is do a select statement for all
distinct rows in T1, then another below it in programming code to select
the desired date range output. Not a subselect, but two selects using


....snip...

Jul 19 '05 #5
BDR,
The query actually contains 6 tables and 5 joins, but that one change,
moving the where condition to the on conditon did the trick!
Thanks for your help.

Dave

"BDR" <jo*@noemail.com> wrote in message
news:3F**************@noemail.com...
Hate responding to my own posts... but I didn't stay at a holiday inn
express last night. This one is much easier than I made it sound
originally. (I just finished a monster multi-join prob yesterday that
threw my head for a loop on this simple one that you posted)..

Move your where condition to the on condition...

SELECT T1.ID, T2.DATES
FROM T1
left join T2
on T1.ID = T2.ID
and ((T2.DATES >= ' 2003-09-11' and T2.DATES <= ' 2003-09-18')
or T2.DATES is null)

ID DATES
A 2003-09-11
B NULL
C NULL
BDR wrote:
Afaik, once you've included a column to be listed in your statement, you
can't 'selectively' eliminate it (or force a display null) in a selected
row result. The way I work around this is do a select statement for all
distinct rows in T1, then another below it in programming code to select
the desired date range output. Not a subselect, but two selects using


...snip...

Jul 19 '05 #6
BDR,
The query actually contains 6 tables and 5 joins, but that one change,
moving the where condition to the on conditon did the trick!
Thanks for your help.

Dave

"BDR" <jo*@noemail.com> wrote in message
news:3F**************@noemail.com...
Hate responding to my own posts... but I didn't stay at a holiday inn
express last night. This one is much easier than I made it sound
originally. (I just finished a monster multi-join prob yesterday that
threw my head for a loop on this simple one that you posted)..

Move your where condition to the on condition...

SELECT T1.ID, T2.DATES
FROM T1
left join T2
on T1.ID = T2.ID
and ((T2.DATES >= ' 2003-09-11' and T2.DATES <= ' 2003-09-18')
or T2.DATES is null)

ID DATES
A 2003-09-11
B NULL
C NULL
BDR wrote:
Afaik, once you've included a column to be listed in your statement, you
can't 'selectively' eliminate it (or force a display null) in a selected
row result. The way I work around this is do a select statement for all
distinct rows in T1, then another below it in programming code to select
the desired date range output. Not a subselect, but two selects using


...snip...

Jul 19 '05 #7

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

Similar topics

0
by: Marek Lewczuk | last post by:
Hello, I have a strange problem, maybe some of you will be able to explain me something. I use LEFT JOIN as a substitute for subselects. It's true that many subselects can be rewriten using LEFT...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
7
by: deko | last post by:
SELECT tblTxAcct.TxAcctName, tblTxType.TxTypeName, Nz(qryTxAcctListCt.TxCount, 0) AS TxCt FROM (tblTxAcct INNER JOIN tblTxType ON tblTxAcct.TxType_ID=tblTxType.TxType_ID) LEFT JOIN qryTxAcctListCt...
2
by: lelandhuang | last post by:
I am developing reporting service and using lots of 'LEFT OUTER JOIN', I am worried about the performance and want to use some subquery to improve the performance. Could I do that like below, ...
0
by: mlarson | last post by:
I have a program that worked fine then they needed to be able to also see the empty cells (inmate cells) on a housing unit when they ran the query. So what I had to do was take two tables and...
3
by: nico3334 | last post by:
I currently have a query that Joins 2 Tables (Table1 and Table2) using LEFT OUTER JOIN. Here is an example of that query: SELECT a.supply, a.state, b.cost FROM Table1 a LEFT...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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,...

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.