Skip to content
Advertisement

SQL Query – second ID of a list ordered by date and ID

I have a SQL database with a list of Customer IDs CustomerID and invoices, the specific product purchased in each invoice ProductID, the Date and the Income of each invoice . I need to write a query that will retrieve for each product, which was the second customer who made a purchase

How do I do that?

EDIT:

I have come up with the following query:

However, this query presents multiple results for products that have more than two purchases. Can you advise?

Advertisement

Answer

I need to write a query that will retrieve for each product, which was the second customer who made a purchase

This sounds like a window function:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement