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!
cdemott33Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
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
Brian CroweDatabase AdministratorCommented:
You can't insert a string as a replacement because they are different datatypes unless you want to change your dates to strings.  Otherwise I would recommend just returning the NULL in this case and handling the messaging in the application.  Change your JOIN to a LEFT OUTER JOIN to get customerGraphicsInStock records regardless of whether they  have a matching customGraphicsOut record.

SELECT       a.header, a.componentID, a.componentName, a.totalQuantity, a.datePosted,  b.max_date
FROM            customGraphicsInStock a
LEFT OUTER 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cdemott33Author Commented:
Thank you!  This worked perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.