Avatar of fb1990
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
SQL

Avatar of undefined
Last Comment
fb1990
Avatar of David Todd
David Todd
Flag of New Zealand image

Hi

Does this work for you?
use AdventureWorks2017
go

select 
	c.CustomerID
	, 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
	c.CustomerID
having 
	count( h.SalesOrderID ) > 0
;

Open in new window



hth
  David
Avatar of fb1990
fb1990

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of fb1990
fb1990

ASKER

Thank you very much, Ryan.  The solution is very helpful!
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo