Data related to the query I'm working on is structured such that
TableA and TableB are 1-many(optional). If an item on TableA has
children on TableB, I need to use the Max(tstamp) from Table B in a
condition, otherwise I need to use a tstamp from TableA (note:there
are additional tables and conditions for this query, but this problem
is based around these 2). I attempted having TableB (as B) "left
outer joined" to TableA, and a condition in the query that resembles:
Where B.Tstamp = (select MAX(tstamp) from TableB
where pkey = B.pkey)
However, the obvious problem, is that when B.Tstamp is Null, the
condition fails because the subselect fails. Is there a way I can get
the subselect to return a Null instead of failing, or a cleaner way to
be able to evaluate this condition and still handle when B.Tstamp is
Null and when it is not?
thanks in advance 5 8197
with t1 (tstamp,pkey) as
(select max(tstamp),pkey from tableb group by pkey)
select a.pkey, coalesce(b.tstamp,a.tstamp)
from tableA a
left outer join t1 b
on b.pkey = a.pkey
will do what you want but may not perform well depending on table sizes.
If the timestamps on tableb are ALWAYS greater than tablea then:
select a.pkey, max(coalesce(b.tstamp,a.tstamp))
from tablea a
left outer join tableb b
on b.pkey = a.pkey
group by a.pkey
You may want to try a global temporary table with an index to get better
performance with the (first example) join. Check explains for different
approaches and see which yields the best performance.
Phil Sherman
Todd wrote: Data related to the query I'm working on is structured such that TableA and TableB are 1-many(optional). If an item on TableA has children on TableB, I need to use the Max(tstamp) from Table B in a condition, otherwise I need to use a tstamp from TableA (note:there are additional tables and conditions for this query, but this problem is based around these 2). I attempted having TableB (as B) "left outer joined" to TableA, and a condition in the query that resembles: Where B.Tstamp = (select MAX(tstamp) from TableB where pkey = B.pkey)
However, the obvious problem, is that when B.Tstamp is Null, the condition fails because the subselect fails. Is there a way I can get the subselect to return a Null instead of failing, or a cleaner way to be able to evaluate this condition and still handle when B.Tstamp is Null and when it is not?
thanks in advance
I think when B.Tstamp is NULL, your subselect returns NULL.
But, "NULL = NULL" is UNKNOWN(not TRUE), then the row will not be
included in result set.
Another consideration is that it might be better to correlate with A,
if possible.
So, my idea is the following:
WHERE (B.tstamp
= (SELECT MAX(tstamp)
FROM TableB
WHERE pkey = A.pkey
)
OR
B.tstamp IS NULL
)
This is another idea.
SELECT ...
, COALESCE(B.tstamp, A.tstamp)
FROM TableA A
LEFT OUTER JOIN
TABLE
(SELECT ...
FROM TableB B
WHERE B.pkey = A.pkey
AND B.tstamp
= (SELECT MAX(tstamp)
FROM TableB Bm
WHERE Bm.pkey = A.pkey
)
) B
ON 0 = 0
If you could select small number of rows in TableA, this would be perform well.
This worked great. Thanks! If the timestamps on tableb are ALWAYS greater than tablea then:
select a.pkey, max(coalesce(b.tstamp,a.tstamp)) from tablea a left outer join tableb b on b.pkey = a.pkey group by a.pkey to********@yahoo.com (Todd) wrote in message news:<28*************************@posting.google.c om>... Data related to the query I'm working on is structured such that TableA and TableB are 1-many(optional). If an item on TableA has children on TableB, I need to use the Max(tstamp) from Table B in a condition, otherwise I need to use a tstamp from TableA (note:there are additional tables and conditions for this query, but this problem is based around these 2). I attempted having TableB (as B) "left outer joined" to TableA, and a condition in the query that resembles: Where B.Tstamp = (select MAX(tstamp) from TableB where pkey = B.pkey)
However, the obvious problem, is that when B.Tstamp is Null, the condition fails because the subselect fails. Is there a way I can get the subselect to return a Null instead of failing, or a cleaner way to be able to evaluate this condition and still handle when B.Tstamp is Null and when it is not?
thanks in advance
I'd rewrite the query using a table expression instead:
FROM
....
(SELECT TABLEA.TABLEA_PK, MAX(TABLEB.TSTAMP) TSTAMP
FROM TABLEA JOIN TABLEB ON TABLEA.TABLEA_PK = TABLEB.TABLEA_PK
GROUP BY TABLEA.TABLEA_PK
UNION ALL
SELECT TABLEA.TABLEA_PK, TABLEA.TSTAMP
FROM TABLEA
WHERE NOT EXISTS(SELECT * FROM TABLEB WHERE TABLEA.TABLEA_PK =
TABLEB.TABLEA_PK)
)MY_EXPRESSION,
....
WHERE TABLEA.TABLEA_PK = MY_EXPRESSION.TABLEA_PK
AND TABLEC.SOME_COLUMN = MY_EXPRESSION.TSTAMP
alternatively, one could use a CASE expression:
WHERE
....
TABLEC.SOME_COLUMN = CASE WHEN EXISTS(
SELECT * FROM TABLEB WHERE TABLEA.TABLEA_PK = TABLEB.TABLEA_PK)
THEN (SELECT MAX(TABLEB.TSTAMP) FROM TABLEB WHERE TABLEA.TABLEA_PK =
TABLEB.TABLEA_PK
ELSE TABLEA.TSTAMP
END
....
Good luck! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Xenophobe |
last post by:
I know this isn't a MySQL forum, but my question is related to a PHP
project.
I have two tables.
table1
table2
"table1" contains 2 columns, ID and FirstName:
|
by: netpurpose |
last post by:
I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use...
|
by: Prem |
last post by:
Hi,
I am having many problems with inner join. my first problem is :
1) I want to know the precedance while evaluating query with multiple
joins.
eg.
select Employees.FirstName,...
|
by: Preston Landers |
last post by:
Hello all.
I am trying to write a query that "just" switches some data around so
it is shown in a slightly different format. I am already able to do
what I want in Oracle 8i, but I am having...
|
by: stu_gots |
last post by:
I have been losing sleep over this puzzle, and I'm convinced my train
of thought is heading in the wrong direction. It is difficult to
explain my circumstances, so I will present an identical...
|
by: thilbert |
last post by:
All,
I have a perplexing problem that I hope someone can help me with.
I have the following table struct:
Permission
-----------------
PermissionId
Permission
|
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"...
|
by: Thomas R. Hummel |
last post by:
Hello,
I am currently working with a data mart. The business wants a view of
the data that shows columns from the main table along with up to 6
codes from a related table (not related by the...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
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...
|
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,...
|
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...
| |