S
SEAN DI''''ANNO
Please help someone...I posed the following question which was answered very
promptly by Gary...The orginal question is entitled Average Buying Cycle. He
answered the question but I am stuggling to convert his answer into the
fields which I use in my database. Would some one mind looking through the
below code which I have titled and give me a final piece of code....All will
be come clear if you read it but despite trying my best...Im stupId.
Open up NorthWind db
and try this query
SELECT
O1.CustomerID,
OD1.ProductID,
O1.OrderDate,
(SELECT Max(O.OrderDate)
FROM
Orders As O
INNER JOIN
[Order Details] As OD
ON O.OrderID=OD.OrderID
WHERE
O.CustomerID = O1.CustomerID
AND
OD.ProductID = OD1.ProductID
AND
O.OrderDate<O1.OrderDate) AS LastOrderDate,
[O1].[OrderDate]-[LastOrderDate] AS BuyingCycle
FROM
Orders AS O1
INNER JOIN
[Order Details] AS OD1
ON O1.OrderID = OD1.OrderID
ORDER BY
O1.CustomerID,
OD1.ProductID,
O1.OrderDate;
look at the results
then in Design View,
under LastOrderDate column in "Criteria" row of grid, type
IS NOT NULL
look at results
save query as "qryBuyingCycle"
Then just save following query to get
your avg buying cycle for each product
(that has been purchased by a customer
more than once)
SELECT
qryBuyingCycle.ProductID,
Avg(qryBuyingCycle.BuyingCycle) AS AvgOfBuyingCycle
FROM qryBuyingCycle
GROUP BY
qryBuyingCycle.ProductID;
BELOW IS THE CODE WHICH i WOULD USE TO GET THE BASIC FIELDS. THE ONLY
ADDITION IS A FIELD CALLED MAJOR WHICH i NEED. IN OTHER WORDS...I AM TRYING
TO WORK OUT THE AVERAGE BUYING CYCLE FOR ANYONE ORDERING FROM THE ASSET
MANAGEMENT PRODUCT RANGE
SELECT dbo_vwfOrders.ContactID, dbo_vwfOrderLines.OrderIdentity,
dbo_vwfOrderLines.ItemID, dbo_vwfOrders.OrderDate
FROM (dbo_vwfOrderLines INNER JOIN dbo_vwfOrders ON
dbo_vwfOrderLines.OrderIdentity = dbo_vwfOrders.OrderIdentity) INNER JOIN
dbo_vwfItems ON dbo_vwfOrderLines.ItemID = dbo_vwfItems.ItemID
WHERE (((dbo_vwfItems.Major)="Asset Managment"));
THIS IS MY ATTEMPT AT TRYING TO CONVERT GARYS CODE. I DID NOT KNOW HOW TO
ADD THE EXTRA FIELD (MAJOR) AND ALSO IN MY QUERY..THE JOIN BETWEEN O1 AND OD1
IS DIFFERENT IN DESIGN VIEW TO THE JOIN IN GARYS ANSWER SO I MHAVE MADE A
MISTAKE SOMEWHERE
SELECT O1.ContactID, OD1.ItemID, O1.OrderDate, (SELECT Max(O.OrderDate)
FROM DBO_VwfOrders As O INNER JOIN [DBO_VwfOrderLines] As OD ON
O.OrderIdentity=OD.OrderIdentity WHERE O.ContactID = O1.ContactID AND
OD.ItemID = OD1.ItemID AND O.OrderDate<O1.OrderDate) AS LastOrderDate,
O1.OrderDate-[LastOrderDate] AS BuyingCycle
FROM DBO_VwfOrders AS O1 INNER JOIN DBO_VwfOrderLines AS OD1 ON
O1.OrderIdentity = OD1.OrderIdentity
WHERE ((((SELECT Max(O.OrderDate) FROM DBO_VwfOrders As O INNER JOIN
[DBO_VwfOrderLines] As OD ON O.OrderIdentity=OD.OrderIdentity WHERE
O.ContactID = O1.ContactID AND OD.ItemID = OD1.ItemID AND
O.OrderDate<O1.OrderDate)) Is Not Null))
ORDER BY O1.ContactID, OD1.ItemID, O1.OrderDate;
promptly by Gary...The orginal question is entitled Average Buying Cycle. He
answered the question but I am stuggling to convert his answer into the
fields which I use in my database. Would some one mind looking through the
below code which I have titled and give me a final piece of code....All will
be come clear if you read it but despite trying my best...Im stupId.
Hi Sean,SEAN DI''''ANNO said:Good morning,
I am stuck trying to create a query for working out a average buying
cycle.
Very simply the task I have bene set is to work out how often customers
buy a
certain product e.g. every 6 months and then produce a calling list of all
customers who ordered the average buying cycle amount ago. e.g. Customers
who
ordered 6 months a go.
It would be easy if customers had only ordered twice becuase then I could
take the second date from the first and then work out the average. The
problem is a lot of customers have ordered more than twice and so I sm
stuck
trying to work out an average buying cyle for these customers. I know in
theory it would be a case of
2nd date - 1st date...3rd date - 2nd date and so but I do not know how
this
would work in a query.....
Open up NorthWind db
and try this query
SELECT
O1.CustomerID,
OD1.ProductID,
O1.OrderDate,
(SELECT Max(O.OrderDate)
FROM
Orders As O
INNER JOIN
[Order Details] As OD
ON O.OrderID=OD.OrderID
WHERE
O.CustomerID = O1.CustomerID
AND
OD.ProductID = OD1.ProductID
AND
O.OrderDate<O1.OrderDate) AS LastOrderDate,
[O1].[OrderDate]-[LastOrderDate] AS BuyingCycle
FROM
Orders AS O1
INNER JOIN
[Order Details] AS OD1
ON O1.OrderID = OD1.OrderID
ORDER BY
O1.CustomerID,
OD1.ProductID,
O1.OrderDate;
look at the results
then in Design View,
under LastOrderDate column in "Criteria" row of grid, type
IS NOT NULL
look at results
save query as "qryBuyingCycle"
Then just save following query to get
your avg buying cycle for each product
(that has been purchased by a customer
more than once)
SELECT
qryBuyingCycle.ProductID,
Avg(qryBuyingCycle.BuyingCycle) AS AvgOfBuyingCycle
FROM qryBuyingCycle
GROUP BY
qryBuyingCycle.ProductID;
BELOW IS THE CODE WHICH i WOULD USE TO GET THE BASIC FIELDS. THE ONLY
ADDITION IS A FIELD CALLED MAJOR WHICH i NEED. IN OTHER WORDS...I AM TRYING
TO WORK OUT THE AVERAGE BUYING CYCLE FOR ANYONE ORDERING FROM THE ASSET
MANAGEMENT PRODUCT RANGE
SELECT dbo_vwfOrders.ContactID, dbo_vwfOrderLines.OrderIdentity,
dbo_vwfOrderLines.ItemID, dbo_vwfOrders.OrderDate
FROM (dbo_vwfOrderLines INNER JOIN dbo_vwfOrders ON
dbo_vwfOrderLines.OrderIdentity = dbo_vwfOrders.OrderIdentity) INNER JOIN
dbo_vwfItems ON dbo_vwfOrderLines.ItemID = dbo_vwfItems.ItemID
WHERE (((dbo_vwfItems.Major)="Asset Managment"));
THIS IS MY ATTEMPT AT TRYING TO CONVERT GARYS CODE. I DID NOT KNOW HOW TO
ADD THE EXTRA FIELD (MAJOR) AND ALSO IN MY QUERY..THE JOIN BETWEEN O1 AND OD1
IS DIFFERENT IN DESIGN VIEW TO THE JOIN IN GARYS ANSWER SO I MHAVE MADE A
MISTAKE SOMEWHERE
SELECT O1.ContactID, OD1.ItemID, O1.OrderDate, (SELECT Max(O.OrderDate)
FROM DBO_VwfOrders As O INNER JOIN [DBO_VwfOrderLines] As OD ON
O.OrderIdentity=OD.OrderIdentity WHERE O.ContactID = O1.ContactID AND
OD.ItemID = OD1.ItemID AND O.OrderDate<O1.OrderDate) AS LastOrderDate,
O1.OrderDate-[LastOrderDate] AS BuyingCycle
FROM DBO_VwfOrders AS O1 INNER JOIN DBO_VwfOrderLines AS OD1 ON
O1.OrderIdentity = OD1.OrderIdentity
WHERE ((((SELECT Max(O.OrderDate) FROM DBO_VwfOrders As O INNER JOIN
[DBO_VwfOrderLines] As OD ON O.OrderIdentity=OD.OrderIdentity WHERE
O.ContactID = O1.ContactID AND OD.ItemID = OD1.ItemID AND
O.OrderDate<O1.OrderDate)) Is Not Null))
ORDER BY O1.ContactID, OD1.ItemID, O1.OrderDate;