Link to home
Start Free TrialLog in
Avatar of CraigDerington
CraigDerington

asked on

DATEDIFF Calculation Where One Argument is NULL

Hello EE,

I am trying to write a query where I can calculate the date difference between the date a widget arrived in my facility and the day it shipped.  The problem is some widgets have to be repaired before they are shipped.  Therefore, some of the widgets have a NULL ship date.  

The query so far...

SELECT WidgetModel, datediff(d, max(DockDate), max(ShipDte)) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (DockDate BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')

I need to be able to add an argument in the query for the NULL shipdates, like:

case when isNULL(shipdte, 'GetDate()') or something to that affect.

Any help is greatly appreciated.

Thanks,

Craig
Avatar of langerking
langerking

Try:

SELECT WidgetModel, datediff(d, max(ShipDte), max(isnull(DockDate, getdate()))) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (ShipDte BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')
Sorry, I made a paste error and also overlooked something in your requirements.  I will post back shortly
Avatar of CraigDerington

ASKER

Actually, your query was absolutely correct, except for the order of the data fields.

I did this and it worked perfectly.

SELECT WidgetModel, datediff(d, max(DockDateDte), max(isnull(ShipDte, getdate()))) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (ShipDte BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')
Group By WidgetModel

I think that's right.  I'll wait for your re-post before awarding the points.

Thanks for your help.

Craig

Your aggregate calculation will give you the difference between the latest DockDate and the latest ShipDate plus 4

Is that what you wanted?  Or were you trying to get the maximum difference between those dates for any single transaction?
I am looking to get the average SLA in days grouped by the Model for everything sent to the facility during a date range.
ASKER CERTIFIED SOLUTION
Avatar of langerking
langerking

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
That works great too.  I believe the average is a much better representation of the actual number of days between the day it arrived in our facility and the day it shipped back out.

What was happening in the aggregate function without the AVG?  It wasn't averaging?

I will await your reply and then award you the points.  Thanks so much for your help with this.

It is very appreciated.

Craig
Before you were taking the diffence between the most recent DockDate and the most recent ShipDate and then adding 4.  In case of a null shipdate, today's date is substituted.


Since they were independantly calculated aggregates (MAX), the calculation is not being made on the same row in your table.

The second method will calculate the difference for each row, again substituing nulls with today's date.  Then the average is calculated on the sum of those differences.  We are also adding 4 to that number.  Why we are adding 4?  I have no idea, but I'm sure you have a reason.
Thank you for the explanation.  That makes a lot of sense.  The reason I am adding 4 is to buffer in the shipment transit time.  Thanks again for all of your help.

Regards,

Craig
Glad I could help!

I see you accepted my answer and even awarded me a grade of "A" (THANK YOU!!) when I once again committed a paste error, leaving my test fields in the answer (Crtd_DateTime, User8).  Thankfully you were able to work with it without missing a beat.
You are very welcome.  I noticed your test fields in the query, so I replaced them with my datafields.  I knew what you meant.  You deserved an A for your immediate response and quick resolution.

This is what makes the EE community great.

Craig
Well you have an understanding for the predicament of experts.  We try to work quickly because as you know, "the early bird gets the worm".  We don't want you to get any worms in your Apps though!

lol

Peter