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

Help with query to extract next best product

I'm having a bit of difficulty getting the results I need from our database.

In a nutshell I'm trying to work out trends in what people buy next. So, for
example, I'm trying to run a query that extracts all orders containing
product1 and then trying to run another query that gives me a count on
products purchased AFTER that product was purchased.

My table relationships are fairy standard for an order processing database:

Customer table (CustomerID) ---> Orders table (OrderID,OrderDate) -->
OrderDetails table (OrderID/ProductID) ---> Products table (ProductID)

I can easily extract buyers of product1 but I'm having difficulty retrieving
the first instance of product1 where product1 has been bought by the same
customer more than once. I've tried grouping by "First" in a "group by"
query but that doesn't give me the first record, it still gives me all of
them. Any pointers or ideas as to how my query should look?

Thanks

Alan
Dec 15 '05 #1
2 1466
On Thu, 15 Dec 2005 12:49:54 +0000 (UTC), "Alan" <no****@nospam.com>
wrote:

I'm not thrilled with First and Last; would use Min and Max instead.
They have a more solid SQL underpinning.

Using the Northwind sample database:
List of customers with the first time they ordered productid=1:
SELECT Orders.CustomerID, Min(Orders.OrderDate) AS MinOfOrderDate
FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order
Details].OrderID
WHERE ((([Order Details].ProductID)=1))
GROUP BY Orders.CustomerID;
Save this as query Q1

Here is Q2: the list of subsequent orders
SELECT Orders.CustomerID, Orders.OrderDate
FROM Q1 INNER JOIN Orders ON Q1.CustomerID = Orders.CustomerID
WHERE (((Orders.OrderDate)>[MinOfOrderDate]));

Q3: Minimum subsequent order:
SELECT Q2.CustomerID, Min(Q2.OrderDate) AS MinOfOrderDate
FROM Q2
GROUP BY Q2.CustomerID;

Q4: The products purchased in the subsequent order:
SELECT Orders.CustomerID, Products.ProductID, Products.ProductName
FROM Q3 INNER JOIN (Products INNER JOIN (Orders INNER JOIN [Order
Details] ON Orders.OrderID = [Order Details].OrderID) ON
Products.ProductID = [Order Details].ProductID) ON (Q3.MinOfOrderDate
= Orders.OrderDate) AND (Q3.CustomerID = Orders.CustomerID);

-Tom.
I'm having a bit of difficulty getting the results I need from our database.

In a nutshell I'm trying to work out trends in what people buy next. So, for
example, I'm trying to run a query that extracts all orders containing
product1 and then trying to run another query that gives me a count on
products purchased AFTER that product was purchased.

My table relationships are fairy standard for an order processing database:

Customer table (CustomerID) ---> Orders table (OrderID,OrderDate) -->
OrderDetails table (OrderID/ProductID) ---> Products table (ProductID)

I can easily extract buyers of product1 but I'm having difficulty retrieving
the first instance of product1 where product1 has been bought by the same
customer more than once. I've tried grouping by "First" in a "group by"
query but that doesn't give me the first record, it still gives me all of
them. Any pointers or ideas as to how my query should look?

Thanks

Alan


Dec 16 '05 #2
Thanks Tom, that gives me exactly what I want! If I turn the last query into
a make table and then run a group by query to count the product names, I can
see what the most popular "next" purchase is.

Also, if I drop Q3 and perform Q4 on Q2 I can find out in general what
people order (not just immediately after) after they've purchased said
product....unless my logic is flawed there?!

Thanks again, this has stopped me going bald with pulling my hair out!

Allan
"Tom van Stiphout" <no*************@cox.net> wrote in message
news:ep********************************@4ax.com...
On Thu, 15 Dec 2005 12:49:54 +0000 (UTC), "Alan" <no****@nospam.com>
wrote:

I'm not thrilled with First and Last; would use Min and Max instead.
They have a more solid SQL underpinning.

Using the Northwind sample database:
List of customers with the first time they ordered productid=1:
SELECT Orders.CustomerID, Min(Orders.OrderDate) AS MinOfOrderDate
FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order
Details].OrderID
WHERE ((([Order Details].ProductID)=1))
GROUP BY Orders.CustomerID;
Save this as query Q1

Here is Q2: the list of subsequent orders
SELECT Orders.CustomerID, Orders.OrderDate
FROM Q1 INNER JOIN Orders ON Q1.CustomerID = Orders.CustomerID
WHERE (((Orders.OrderDate)>[MinOfOrderDate]));

Q3: Minimum subsequent order:
SELECT Q2.CustomerID, Min(Q2.OrderDate) AS MinOfOrderDate
FROM Q2
GROUP BY Q2.CustomerID;

Q4: The products purchased in the subsequent order:
SELECT Orders.CustomerID, Products.ProductID, Products.ProductName
FROM Q3 INNER JOIN (Products INNER JOIN (Orders INNER JOIN [Order
Details] ON Orders.OrderID = [Order Details].OrderID) ON
Products.ProductID = [Order Details].ProductID) ON (Q3.MinOfOrderDate
= Orders.OrderDate) AND (Q3.CustomerID = Orders.CustomerID);

-Tom.
I'm having a bit of difficulty getting the results I need from our database.
In a nutshell I'm trying to work out trends in what people buy next. So, forexample, I'm trying to run a query that extracts all orders containing
product1 and then trying to run another query that gives me a count on
products purchased AFTER that product was purchased.

My table relationships are fairy standard for an order processing database:
Customer table (CustomerID) ---> Orders table (OrderID,OrderDate) -->
OrderDetails table (OrderID/ProductID) ---> Products table (ProductID)

I can easily extract buyers of product1 but I'm having difficulty retrievingthe first instance of product1 where product1 has been bought by the same
customer more than once. I've tried grouping by "First" in a "group by"
query but that doesn't give me the first record, it still gives me all of
them. Any pointers or ideas as to how my query should look?

Thanks

Alan

Dec 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
0
by: B. Fongo | last post by:
------=_NextPart_000_0011_01C36322.2FEF5DC0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable It works!=20 SELECT Customers.Name, Customers.City,...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
4
by: Macroman | last post by:
MS Access XP, running on Win XP, Processor 2.4Ghz , 512Mb RAM, 40Gb Hard drive Table 1 has 167,000 records and contains the following fields tblone_custID tblone_easting tblone_northing ...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.