Avatar of fb1990
 asked on

Finding Customers who made a purchase and customer who did not make a purchase

Hello EE,

Can someone please help me with a SQL code to get a list of customers who purchased at least 1 item in a giving month and customers who did not make a purchase in that same month? I want to show 1 for customers who made a purchase and 0 for customers who did not make a purchase.  I have 2 tables - a customer table and an order table.  The customer table contains all the customers while the order table contains only the customer who made a purchase in any giving month.  In my case, I want to show customers who made a purchase in the month of January 2020 customers who did not.

Both tables have the customer as an identifier while the orders table contains customerno, ordercount, and orderdate

Avatar of undefined
Last Comment

8/22/2022 - Mon
David Todd


Does this work for you?
use AdventureWorks2017

	, count( h.SalesOrderID )
from sales.Customer c
left outer join sales.SalesOrderHeader h
	on h.CustomerID = c.CustomerID
	and h.OrderDate >= '2012-01-01'
	and h.OrderDate < '2012-02-01'
group by
	count( h.SalesOrderID ) > 0

Open in new window


Thank you for your assistance.  I did not see where this is referencing the customer table. I need to represent customers in the order table who made a purchase with 1 and customers who did not make a purchase, but on the customer table with a 0 in the result
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thank you very much, Ryan.  The solution is very helpful!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy