Link to home
Start Free TrialLog in
Avatar of cdemott33
cdemott33Flag for United States of America

asked on

T-SQL Syntax help needed. Using JOIN with NULLS

I'm having some issues with the code below.  The code executes but the problem is this.

The b.max_date column get it's data (date) from the SELECT statement after the JOIN.  If the is no max(arrivalDate) found the query excludes that record from the results.

Is there way to write the SELECT so that, if there isn't a max(arrivalDate) (i.e. the value is NULL) it would included the record with a note in the column stating "no date found"?


SELECT       a.header, a.componentID, a.componentName, a.totalQuantity, a.datePosted,  b.max_date
FROM            customGraphicsInStock a
Join
(SELECT componentID, max(arrivalDate) as max_date FROM customGraphicsOut group by componentID) b on a.componentID = b.componentID
WHERE a.active = 'yes'
ORDER by a.header

Open in new window


Thanks in advance for your help!
Avatar of lcohan
lcohan
Flag of Canada image

Please check SET ANSI NULLs settings or ISNULL/COLEASCE functions to help you with that like:

SELECT       a.header, a.componentID, a.componentName, a.totalQuantity, a.datePosted,  b.max_date
FROM            customGraphicsInStock a
Join
(SELECT componentID, max(isnull(arrivalDate, '1900-01-01')) as max_date FROM customGraphicsOut group by componentID) b on a.componentID = b.componentID
WHERE a.active = 'yes'
ORDER by a.header
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cdemott33

ASKER

Thank you!  This worked perfectly.