|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Difference of values from the same columnCan anybody help me in finding the difference of values (value1-value2,...) from the same column of a table. Suppose, OrderDate column of Orders table has values like 07/01/05,07/05/05,07/08/05,07/12/05. How to find the no of days between two order dates using a query? What I am trying to do is, to find the frequency with which the orders are getting placed by a customer. Though the no of days between any two dates need not be same, I want to take average of no of days between two orders so that, it can be considered as avg frequency of orders for a customer. Any sort of help is appreciated. Thanks Srinivas Use DATEDIFF() system function.For more details please refer to the BOL
<mlsrini***@gmail.com> wrote in message Show quote news:1123837159.780460.215190@f14g2000cwb.googlegroups.com... > Hi, > > Can anybody help me in finding the difference of values > (value1-value2,...) from the same column of a table. > > Suppose, OrderDate column of Orders table has values like > 07/01/05,07/05/05,07/08/05,07/12/05. How to find the no of days between > two order dates using a query? > > What I am trying to do is, to find the frequency with which the orders > are getting placed by a customer. Though the no of days between any two > dates need not be same, I want to take average of no of days between > two orders so that, it can be considered as avg frequency of orders for > a customer. > > Any sort of help is appreciated. > > Thanks > Srinivas > To help you with that more information than this you gave use is necessary,
because a customer could also place several orders at one day which would effect the result in a respective way. So please post some ddl to help you to get the correct result. Show quote "mlsrini***@gmail.com" wrote: > Hi, > > Can anybody help me in finding the difference of values > (value1-value2,...) from the same column of a table. > > Suppose, OrderDate column of Orders table has values like > 07/01/05,07/05/05,07/08/05,07/12/05. How to find the no of days between > two order dates using a query? > > What I am trying to do is, to find the frequency with which the orders > are getting placed by a customer. Though the no of days between any two > dates need not be same, I want to take average of no of days between > two orders so that, it can be considered as avg frequency of orders for > a customer. > > Any sort of help is appreciated. > > Thanks > Srinivas > > Try this (untested):
SELECT customer, DATEDIFF(DAY,MIN(orderdate), MAX(orderdate)) / CAST(COUNT(*) AS REAL) AS avg_days_between_orders FROM Orders GROUP BY customer If you need more help, please post DDL and sample data. -- David Portas SQL Server MVP -- |
|||||||||||||||||||||||