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

DateDiff... with group by & AVG

12
Hi,

I am a little stuck with a query I am trying to build. Attemting to calculate the delivery time for a material.

I have managed to utilise the DateDiff function, but have some additional complexities.

1. The dates I am using are to calculate a more correct delivery time is not always populated, therefore I need to utilise a different date field.

Expand|Select|Wrap|Line Numbers
  1. DateDiff("d",DTMORD,DATREC) AS Expr1
  2. DateDiff("d",DTMORL,DATREC) AS Expr2
I attempted to use a IIf function with this so that if DTMORL is Null, then use DTMORD. But I kept getting errors.

2. I also need to AVG and Group the results by Material, therefore my current query is a little messy. I have 2 columns, 1 for each calculation but really only want to AVG a single result as NULL entries confuse the mix.

Any help appreciated.

Stevo
Aug 22 '07 #1
9 2804
Rabbit
12,516 Expert Mod 8TB
Hi,

I am a little stuck with a query I am trying to build. Attemting to calculate the delivery time for a material.

I have managed to utilise the DateDiff function, but have some additional complexities.

1. The dates I am using are to calculate a more correct delivery time is not always populated, therefore I need to utilise a different date field.

Expand|Select|Wrap|Line Numbers
  1. DateDiff("d",DTMORD,DATREC) AS Expr1
  2. DateDiff("d",DTMORL,DATREC) AS Expr2
I attempted to use a IIf function with this so that if DTMORL is Null, then use DTMORD. But I kept getting errors.

2. I also need to AVG and Group the results by Material, therefore my current query is a little messy. I have 2 columns, 1 for each calculation but really only want to AVG a single result as NULL entries confuse the mix.

Any help appreciated.

Stevo
1) It would help to know what iif statement you tried to use and what error you were getting.

2) The solution to this depends on getting the iif statement correct.
Aug 22 '07 #2
StevoNZ
12
1) It would help to know what iif statement you tried to use and what error you were getting.

2) The solution to this depends on getting the iif statement correct.
Hi, thanks for the quick response.
Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull(DTMORL,DateDiff("d",DTMORD,DATREC),DateDiff("d",DTMORL,DATREC),Null) AS DelTime
The error message I receive is: Syntax error (missing operator) in query expression 'IIf...

I think I've resolved my own issue by adding () around first object in statement:
Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([DTMORL]),DateDiff("d",DTMORD,DATREC),DateDiff("d",DTMORL,DATREC)) AS DelTime
Aug 22 '07 #3
Rabbit
12,516 Expert Mod 8TB
Yes, that indeed was the problem, with that fixed you should have no problem making the query.
Aug 22 '07 #4
StevoNZ
12
Yes, that indeed was the problem, with that fixed you should have no problem making the query.
This resolved my original problem, but now I encounter another... some date entries were loaded from converted data from a previous legacy system & the DateDiff calculation is correctly reporting negative result.

How can I include the Null values (via the IIf statement) so that if DTMORL is Null, then calculate with DTMORD, but also exclude negative results? If I use a criteria such as >0, then the Null values are already excluded.

My thoughts are to create a subsequery and calculate a DateDiff result for both DTMORL & DTMORD, group result by material number and AVG the DateDiff calculations. Only problem is, I don't really know how to create subquery's!
Aug 24 '07 #5
Rabbit
12,516 Expert Mod 8TB
This resolved my original problem, but now I encounter another... some date entries were loaded from converted data from a previous legacy system & the DateDiff calculation is correctly reporting negative result.

How can I include the Null values (via the IIf statement) so that if DTMORL is Null, then calculate with DTMORD, but also exclude negative results? If I use a criteria such as >0, then the Null values are already excluded.

My thoughts are to create a subsequery and calculate a DateDiff result for both DTMORL & DTMORD, group result by material number and AVG the DateDiff calculations. Only problem is, I don't really know how to create subquery's!
I don't understand. If DTMORL is null and it uses DTMORD instead, you shouldn't have any null values anyways. So there should be no problem using > 0 as the criteria.
Aug 24 '07 #6
StevoNZ
12
I don't understand. If DTMORL is null and it uses DTMORD instead, you shouldn't have any null values anyways. So there should be no problem using > 0 as the criteria.
Sorry for any confusion, where DTMORL has a value the DateDiff function is sometimes calculating a negative result. So, in this case I want to exclude these negative results and use DTMORD for DateDiff calculation instead.
Aug 24 '07 #7
Rabbit
12,516 Expert Mod 8TB
I see, use:
IsNull([DTMORL]) OR DTMORD > DATREC
Aug 24 '07 #8
StevoNZ
12
I see, use:
IsNull([DTMORL]) OR DTMORD > DATREC
Thanks all resolved!!

Although code should actually read:
Expand|Select|Wrap|Line Numbers
  1. IsNull(DTMORL) OR (DTMORL>DATREC)
Aug 27 '07 #9
Rabbit
12,516 Expert Mod 8TB
Not a problem, good luck.
Aug 27 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: inamori | last post by:
I face that problems 07/01/2003 06/30/2006 ---------> it should be 3 01/01/2003 02/28/2005 --------->could i get 2 years and 2 months 01/01/2003 03/01/2005 ...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
0
by: Kevin | last post by:
Group By - Client Sum - Past:IIF(DateDiff("d",,Date())>30,,0) Sum - Current:IIF(DateDiff("d",,Date())<31,,0) Works for positive numbers, but if InvoiceNetTotal = ($4.00) and InvoiceNetTotal =...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
4
by: Brian Henry | last post by:
I am trying to find someones age within 2 months of their birthday on the given date for the run of the application Dim d_runOn As DateTime = #4/4/2005# Dim d_bday As DateTime = #6/26/1986# ...
12
by: TofuTheGreat | last post by:
I did a group search and found hundreds of cases of problems with DateDiff(). I read through several dozen but they didn not apply to my situation. Not having the time to read through all of them...
5
by: BUmed | last post by:
I want to set up a Query to show the diffence between 2 dates and the output to be in months and weeks. So the child is 15months and 2weeks (15/2). I have been try to modify DateDiff Age:...
6
by: ndeeley | last post by:
Hello! I have a `job` database, where users add a job (or record) with a start date and end date. I need to run a query that will show me the number of uncompleted jobs by user - which I'm...
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:
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...
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...
0
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
0
BarryA
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...
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
marktang
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,...
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...

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.