Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-22 : 11:29:21
|
I am going to provide mass amounts of data here in hopes that I will not omitt anything needed. Bare with me.I work for a courier service. I am trying to write a query that will show us the difference in scheduled route times verse actual route times. Here is the script that I have so far.SelectCustomer.Name As CustName,min(ordermain.orderdate) AS StartDate,max(ordermain.orderdate) As EndDate,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End as RteName,min(Js2.sequence) As Stops,count(ordermain.OrderID) As Job_Count,min(js1.scheduleddatetime) As First_Stop_SchedTime,min(js2.scheduleddatetime) As Last_Stop_SchedTime,Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End As First_Stop_Actual_Time,Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End As Last_Stop_Actual_TIme,Case When Sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) End As First_Stop_Minutes_Late_Early,Case When Sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) End As Last_Stop_Minutes_Late_EarlyFromOrdermain with (readuncommitted, INDEX=OrderDateOrderID)Left Outer Join Customer with (readuncommitted) on Ordermain.CustomerID=Customer.CustomerIDLeft Outer Join Job with (readuncommitted) on Ordermain.OrderID=Job.OrderIDLeft Outer Join Jobstop JS2 with (readuncommitted, INDEX=JobIDSequence) on JS2.JobID=JOb.JobID and JS2.Sequence=(Select Max(Sequence) From Jobstop JS3 where JS3.JobID=Job.jobID)Left Outer Join Jobstop JS1 with (readuncommitted, INDEX=JobIDSequence) On JS1.JobID=Job.JobidWhere Ordermain.OrderStatus IN ('N', 'A', 'I', 'P') and Ordermain.OrderDate Between '05/04/08' and '05/10/08' and Customer.CustomerCode='331' and left(job.service,3)='Rou' and js1.sequence=1Group By Customer.Name, Ordermain.orderdate, Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End Order By Customer.Name, Ordermain.Orderdate, Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber EndHOWEVER, MY RETURN SET IS SHOWING EACH DAYS ROUTES WHERE I WANT ONLY ONE LINE PER ROUTE. ANY HELP WOULD BE GREATLY APPRECIATED |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 11:34:32
|
Any special reason why you're using read uncommited hint? ALso can you please post the tables involved with some sample data from each and specify what you want off them? |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-22 : 12:02:13
|
Here is a sample return set for the above query. (I use readuncommited so I do not slow down the server....thats what I have been told to do)Another small thing...I am 100% self taught here and I may be missing the simplest of stuff. Here is the sample return set. I have abbreviated some headings so that it will read easier. Cust StartDate EndDate Rte Stops JobCount FirstSched LastSched FirstActual LastActual FstDiff LstDiff XX 5/5/2008 5/5/2008 1 9 1 DateTime DateTime DateTime DateTime Minutes Minutes XX 5/6/2008 5/6/2008 1 9 1 DateTime DateTime DateTime DateTime Minutes MinutesHere we have the same route (1) on different days. I need to have this info return on one line with an average for the FirstDiff and LastDiff columns. The job count should then read 2 and I will divide the Minutes different (firstdiff, lastdiff) by the job count to get the average. I will be more than happy to supple all tables and examples if someone wants to contact me via email. jtaylor@networkexpressinc.com |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 12:25:09
|
quote: Originally posted by henryparsons10 Here is a sample return set for the above query. (I use readuncommited so I do not slow down the server....thats what I have been told to do)Another small thing...I am 100% self taught here and I may be missing the simplest of stuff. Here is the sample return set. I have abbreviated some headings so that it will read easier. Cust StartDate EndDate Rte Stops JobCount FirstSched LastSched FirstActual LastActual FstDiff LstDiff XX 5/5/2008 5/5/2008 1 9 1 DateTime DateTime DateTime DateTime Minutes Minutes XX 5/6/2008 5/6/2008 1 9 1 DateTime DateTime DateTime DateTime Minutes MinutesHere we have the same route (1) on different days. I need to have this info return on one line with an average for the FirstDiff and LastDiff columns. The job count should then read 2 and I will divide the Minutes different (firstdiff, lastdiff) by the job count to get the average. I will be more than happy to supple all tables and examples if someone wants to contact me via email. jtaylor@networkexpressinc.com
then keep only the fields Customer.Name,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End in the group by and drops others and try. may be like this:-SelectCustomer.Name As CustName,min(ordermain.orderdate) AS StartDate,max(ordermain.orderdate) As EndDate,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End as RteName,min(Js2.sequence) As Stops,count(ordermain.OrderID) As Job_Count,min(js1.scheduleddatetime) As First_Stop_SchedTime,min(js2.scheduleddatetime) As Last_Stop_SchedTime,Case When Sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) End As First_Stop_Minutes_Late_Early,Case When Sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) End As Last_Stop_Minutes_Late_EarlyFromOrdermain with (readuncommitted, INDEX=OrderDateOrderID)Left Outer Join Customer with (readuncommitted) on Ordermain.CustomerID=Customer.CustomerIDLeft Outer Join Job with (readuncommitted) on Ordermain.OrderID=Job.OrderIDLeft Outer Join Jobstop JS2 with (readuncommitted, INDEX=JobIDSequence) on JS2.JobID=JOb.JobID and JS2.Sequence=(Select Max(Sequence) From Jobstop JS3 where JS3.JobID=Job.jobID)Left Outer Join Jobstop JS1 with (readuncommitted, INDEX=JobIDSequence) On JS1.JobID=Job.JobidWhereOrdermain.OrderStatus IN ('N', 'A', 'I', 'P') andOrdermain.OrderDate Between '05/04/08' and '05/10/08' andCustomer.CustomerCode='331' andleft(job.service,3)='Rou' andjs1.sequence=1Group ByCustomer.Name,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber EndOrder ByCustomer.Name, Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-22 : 12:46:53
|
See now...thats what gets me. I will study this for an hour to find out the differences. Either way...thanks a million. It works great. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 12:54:08
|
quote: Originally posted by henryparsons10 See now...thats what gets me. I will study this for an hour to find out the differences. Either way...thanks a million. It works great.
you're welcome . basically what ever field you want aggregation to happen over, you group by them and apply aggregate function over other fields to get your required values. |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-29 : 13:31:04
|
Okay. I have this working now so that it returns the following set. Cust Start End Rte Stops Job_Count FirstSchedTime LastSchedTime AvgFirsLate_Earl AvgLastLate_EarlyWilson 5/5/2008 5/9/2008 1 9 5 5/5/2008 20:30 5/6/2008 1:05 26 34Wilson 5/5/2008 5/9/2008 3 17 5 5/5/2008 20:30 5/6/2008 4:50 31 Wilson 5/5/2008 5/9/2008 4 17 5 5/5/2008 20:30 5/6/2008 6:13 26 -26Wilson 5/5/2008 5/9/2008 5 19 5 5/5/2008 20:30 5/6/2008 7:24 35 12This is my querySelectCustomer.Name As CustName,min(ordermain.orderdate) AS StartDate,max(ordermain.orderdate) As EndDate,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End as RteName,min(Js2.sequence) As Stops,count(ordermain.OrderID) As Job_Count,min(js1.scheduleddatetime) As First_Stop_SchedTime,min(js2.scheduleddatetime) As Last_Stop_SchedTime,Case When Sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) End As First_Stop_Minutes_Late_Early,Case When Sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) End As Last_Stop_Minutes_Late_EarlyFromOrdermain with (readuncommitted, INDEX=OrderDateOrderID)Left Outer Join Customer with (readuncommitted) on Ordermain.CustomerID=Customer.CustomerIDLeft Outer Join Job with (readuncommitted) on Ordermain.OrderID=Job.OrderIDLeft Outer Join Jobstop JS2 with (readuncommitted, INDEX=JobIDSequence) on JS2.JobID=JOb.JobID and JS2.Sequence=(Select Max(Sequence) From Jobstop JS3 where JS3.JobID=Job.jobID)Left Outer Join Jobstop JS1 with (readuncommitted, INDEX=JobIDSequence) On JS1.JobID=Job.JobidWhereOrdermain.OrderStatus IN ('N', 'A', 'I', 'P') andOrdermain.OrderDate Between '05/04/08' and '05/10/08' andCustomer.CustomerCode='331' andleft(job.service,3)='Rou' andjs1.sequence=1Group ByCustomer.Name,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber EndOrder ByCustomer.Name, Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber EndThe query returns the first stop scheduled time for a route, and the first stop avg minutes late/early real time. It returns the same info for the last stop. I need to sum the difference so that I can get an average route length. I am trying to do it with a subquery but I get the internal server error. Any help with what I have already or what needs to be done will help tremendously. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-29 : 13:45:30
|
What would be the calculation formula for average route length? |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-29 : 13:52:10
|
It would be first DateDiff((First stop scheduled time +/- avgfirststopdiff), last stop scheduled time +/- avglaststopdiff)) As Average Route Time |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-29 : 14:09:00
|
then try like thisSelect *,--put field names here DATEDIFF(mi,DATEADD(mi,First_Stop_Minutes_Late_Early,First_Stop_SchedTime),DATEADD(mi,Last_Stop_Minutes_Late_Early,Last_Stop_SchedTime)) AS AverageRoutTimefrom(SelectCustomer.Name As CustName,min(ordermain.orderdate) AS StartDate,max(ordermain.orderdate) As EndDate,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End as RteName,min(Js2.sequence) As Stops,count(ordermain.OrderID) As Job_Count,min(js1.scheduleddatetime) As First_Stop_SchedTime,min(js2.scheduleddatetime) As Last_Stop_SchedTime,Case When Sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js1.poddatetime IS NULL then js1.arrivedatetime Else js1.poddatetime End,js1.scheduleddatetime)+0) End As First_Stop_Minutes_Late_Early,Case When Sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) Between -480 and 480 Then sum(DateDiff(minute, Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End,js2.scheduleddatetime)+0) End As Last_Stop_Minutes_Late_EarlyFromOrdermain with (readuncommitted, INDEX=OrderDateOrderID)Left Outer Join Customer with (readuncommitted) on Ordermain.CustomerID=Customer.CustomerIDLeft Outer Join Job with (readuncommitted) on Ordermain.OrderID=Job.OrderIDLeft Outer Join Jobstop JS2 with (readuncommitted, INDEX=JobIDSequence) on JS2.JobID=JOb.JobID and JS2.Sequence=(Select Max(Sequence) From Jobstop JS3 where JS3.JobID=Job.jobID)Left Outer Join Jobstop JS1 with (readuncommitted, INDEX=JobIDSequence) On JS1.JobID=Job.JobidWhereOrdermain.OrderStatus IN ('N', 'A', 'I', 'P') andOrdermain.OrderDate Between '05/04/08' and '05/10/08' andCustomer.CustomerCode='331' andleft(job.service,3)='Rou' andjs1.sequence=1Group ByCustomer.Name,Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End)tOrder ByCustomer.Name, Case When Job.RouteNumber IS NULL Then Ordermain.Auth Else Job.RouteNumber End |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-29 : 14:24:36
|
Still giving me the internal SQL Server Error. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-29 : 14:31:34
|
quote: Originally posted by henryparsons10 Still giving me the internal SQL Server Error.
I've never got or seen this error before. Just googled and got this:-http://support.microsoft.com/kb/830466 |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-29 : 14:36:29
|
thx...I will check into the fix. However, its a shared server which ALWAYS causes issue for us. Either way, it looks like this is something that needs to be done anyway. |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-05-29 : 15:12:43
|
Okay, I have decided to go another route. I have this argument here that will return the average date time value for the first and last stops. CAST(AVG(Cast(Case when js1.poddatetime IS Null then js1.arrivedatetime Else js1.poddatetime End As float)) As DateTime) As First_Stop_AvgTime,CAST(AVG(Cast(Case when js2.poddatetime IS NULL then js2.arrivedatetime Else js2.poddatetime End As float)) As Datetime) As Last_Stop_AvgTimeHowever, we sometimes have issues with couriers not setting the correct time on their units so we have really odd POD (proof of delivery) times. I need to add a clause to the statement(s) above that would disregaurd any POD time that is more than eight hours (-480 minutes or +480 minutes) from the scheduled date time. Which is js1.scheduleddatetime (fist stop) and js2.scheduleddatetime (last stop)Any ideas? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-30 : 13:18:34
|
CAST(AVG(Cast(Case when js1.poddatetime IS Null then js1.arrivedatetime Else case when ABS(datediff(mi,js1.scheduleddatetime,js1.poddatetime))<480 then js1.poddatetime ELSE NULL END End As float)) As DateTime) As First_Stop_AvgTime,CAST(AVG(Cast(Case when js2.poddatetime IS NULL then js2.arrivedatetime Else case when ABS(datediff(mi,js2.scheduleddatetime,js2.poddatetime))<480 then js2.poddatetime ELSE NULL END End As float)) As Datetime) As Last_Stop_AvgTime |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-06-02 : 11:38:34
|
Everything is working great with this query so far. Except, where it returns average route length as hours and minutes, they now want to see it as a decimal. I am getting the "cannot convert varchar to numberic" with everything I try.Here it is:left(convert(char,CAST(AVG(Cast(Case when js2.poddatetime IS NULL then js2.arrivedatetime Else case when ABS(datediff(mi,js2.scheduleddatetime,js2.poddatetime))<480 then js2.poddatetime ELSE js2.scheduleddatetime END End As float)) As Datetime)-CAST(AVG(Cast(Case when js1.poddatetime IS Null then js1.arrivedatetime Else case when ABS(datediff(mi,js1.scheduleddatetime,js1.poddatetime))<480 thenjs1.poddatetime ELSE js1.scheduleddatetime END End As float)) As DateTime),14),5) As Avg_Route_LengthHow can I get a decimal returned here? I swear, last one and its good to go. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-02 : 12:09:37
|
quote: Originally posted by henryparsons10 Everything is working great with this query so far. Except, where it returns average route length as hours and minutes, they now want to see it as a decimal. I am getting the "cannot convert varchar to numberic" with everything I try.Here it is:left(convert(char,CAST(AVG(Cast(Case when js2.poddatetime IS NULL then js2.arrivedatetime Else case when ABS(datediff(mi,js2.scheduleddatetime,js2.poddatetime))<480 then js2.poddatetime ELSE js2.scheduleddatetime END End As float)) As Datetime)-CAST(AVG(Cast(Case when js1.poddatetime IS Null then js1.arrivedatetime Else case when ABS(datediff(mi,js1.scheduleddatetime,js1.poddatetime))<480 thenjs1.poddatetime ELSE js1.scheduleddatetime END End As float)) As DateTime),14),5) As Avg_Route_LengthHow can I get a decimal returned here? I swear, last one and its good to go.
Are you sure its always returning numeric values? If yes, you could use CAST or CONVERT to convert it. |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-06-02 : 12:57:38
|
ExampleAvg_Route_Length7:587:452:046:296:58It returns hours and minutes, like above. I want the decimal. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-02 : 13:04:59
|
quote: Originally posted by henryparsons10 ExampleAvg_Route_Length7:587:452:046:296:58It returns hours and minutes, like above. I want the decimal.
Didnt understand why you're trying to convert time to numeric. |
 |
|
henryparsons10
Starting Member
23 Posts |
Posted - 2008-06-02 : 13:19:41
|
Because our route planning department uses 7.5 for 7:30, and they want the data in the same format. Also, I have a scheduled length column that is returning the data as a decimal and I need both to be the same. Here is my scheduled length;round(DateDiff(minute,max(js1.latedatetime),max(js2.latedatetime)+0)/.6/100,2) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-02 : 13:27:10
|
quote: Originally posted by henryparsons10 Because our route planning department uses 7.5 for 7:30, and they want the data in the same format. Also, I have a scheduled length column that is returning the data as a decimal and I need both to be the same. Here is my scheduled length;round(DateDiff(minute,max(js1.latedatetime),max(js2.latedatetime)+0)/.6/100,2)
may be this will work, but you wont get 7.5 for 7:30, it will just give you 7.3cast(replace(left(convert(char,CAST(AVG(Cast(Case when js2.poddatetime IS NULL then js2.arrivedatetime Else case when ABS(datediff(mi,js2.scheduleddatetime,js2.poddatetime))<480 then js2.poddatetime ELSE js2.scheduleddatetime END End As float)) As Datetime)-CAST(AVG(Cast(Case when js1.poddatetime IS Null then js1.arrivedatetime Else case when ABS(datediff(mi,js1.scheduleddatetime,js1.poddatetime))<480 thenjs1.poddatetime ELSE js1.scheduleddatetime END End As float)) As DateTime),14),5),':','.') AS numeric(10,2)) As Avg_Route_Length |
 |
|
Next Page
|
|
|
|
|