473,789 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,OrderD ate) -->
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 1480
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.Customer ID, Min(Orders.Orde rDate) AS MinOfOrderDate
FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order
Details].OrderID
WHERE ((([Order Details].ProductID)=1))
GROUP BY Orders.Customer ID;
Save this as query Q1

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

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

Q4: The products purchased in the subsequent order:
SELECT Orders.Customer ID, Products.Produc tID, Products.Produc tName
FROM Q3 INNER JOIN (Products INNER JOIN (Orders INNER JOIN [Order
Details] ON Orders.OrderID = [Order Details].OrderID) ON
Products.Produc tID = [Order Details].ProductID) ON (Q3.MinOfOrderD ate
= Orders.OrderDat e) AND (Q3.CustomerID = Orders.Customer ID);

-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,OrderD ate) -->
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....unle ss 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.c om...
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.Customer ID, Min(Orders.Orde rDate) AS MinOfOrderDate
FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order
Details].OrderID
WHERE ((([Order Details].ProductID)=1))
GROUP BY Orders.Customer ID;
Save this as query Q1

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

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

Q4: The products purchased in the subsequent order:
SELECT Orders.Customer ID, Products.Produc tID, Products.Produc tName
FROM Q3 INNER JOIN (Products INNER JOIN (Orders INNER JOIN [Order
Details] ON Orders.OrderID = [Order Details].OrderID) ON
Products.Produc tID = [Order Details].ProductID) ON (Q3.MinOfOrderD ate
= Orders.OrderDat e) AND (Q3.CustomerID = Orders.Customer ID);

-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,OrderD ate) -->
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
3482
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 Pentagjetvedeh karuvificials madhla reachathe strategy in karkun campaign deshatinst terrorism. "mudivae maretu winning or losing karkun global varti jetvedeh terror?" Mr. Rumsfeld adugued in a recent memormariyuum. vede velli jetvedeh madhla...
0
1546
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, Orders.Product, Order.Price FROM Customers INNER JOIN Orders ON Customers.cust_id =3D Orders.cust_id WHERE cust_id =3D 2;
9
3138
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 SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
22
3068
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 3, NULL, 9, 82, 25
9
4356
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 predecessor, I hasten to add) so that each day it creates a copy of the record for each company, changes the date to today's date, and prompts the user for any changes of ratings on that day. The resulting data table grows by approx 600 records per...
3
3615
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 in the subform. Is it possible? How do I do this? Thanks in advance. Paul from Slovakia
4
3715
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 Table 2 has 423,000 records and contains the following fields tbltwo_custID
16
2820
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
0
5576
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.